zoukankan      html  css  js  c++  java
  • 字符串与基本数据的相互转换

    (1)java.lang包中的Integer类调用其类方法public static int parseInt(String s)可以将数字格式的字符串转换为int型数据,用例如下:

    int x;  String s="123456";   x=Integer.parseInt(s);

    (2)类似的,使用java.lang包中的Byte、Short、Long、Float、Double类调用相应的类方法:

    public static byte parseByte(String s) throws NumberFormatException

    public static short parseShort(String s) throws NumberFormatException

    public static long parseLong(String s) throws NumberFormatException

    public static float parseFloat(String s) throws NumberFormatException

    public static double parseDouble(String s) throws NumberFormatException

    将数字类格式的字符串转换为相应的基本数据类型

    (3)可以使用String类的下列方法:

    public static String valueOf(byte n) 

    public static String valueOf(int n) 

    public static String valueOf(short n) 

    public static String valueOf(long n) 

    public static String valueOf(float n) 

    public static String valueOf(double n) 将数值转换为字符串,格式如下:

    String str=String.valueOf(1234.789);

    (4)可以使用Long类的下列类方法得到整型的各种进制的字符串表示。

    public static String toBinaryString(long i)

    public static String toOctalString(long i)

    public static String toHexString(long i)

    public static String toString(long i,int p)//p进制

    样例如下:

  • 相关阅读:
    JS函数防抖与函数节流
    AJAX问题 XMLHttpRequest.status = 0是什么含义
    通过JS如何获取IP地址
    关于URL编码
    报错Unexpected token u
    css文本超出2行就隐藏并且显示省略号
    At_speed_test
    Logic Bist Arch
    Logic BIST
    DMA-330(二)
  • 原文地址:https://www.cnblogs.com/zengfanye/p/9406403.html
Copyright © 2011-2022 走看看