zoukankan      html  css  js  c++  java
  • Java下数字类型的转换

    各种数字类型转换成字符串型:

    String s = String.valueOf( value); // 其中 value 为任意一种数字类型。

    字符串型转换成各种数字类型:

    String s = "169";
    byte b = Byte.parseByte( s );
    short t = Short.parseShort( s );
    int i = Integer.parseInt( s );
    long l = Long.parseLong( s );
    Float f = Float.parseFloat( s );
    Double d = Double.parseDouble( s );

    数字类型与数字类对象之间的转换:

    byte b = 169;
    Byte bo = new Byte( b );
    b = bo.byteValue();

    short t = 169;
    Short to = new Short( t );
    t = to.shortValue();

    int i = 169;
    Integer io = new Integer( i );
    i = io.intValue();

    long l = 169;
    Long lo = new Long( l );
    l = lo.longValue();

    float f = 169f;
    Float fo = new Float( f );
    f = fo.floatValue();

    double d = 169f;
    Double dObj = new Double( d );
    d = dObj.doubleValue();

    实际应用中还有可能会用到各数字类型与字节数组间的转换, Java 没有直接提供这样的支持。这里我编写了一个这样的转换工具。供大家参考使用:NumberTool.java 使用时请注意,数字类型转换为字节数组时,存放的顺序应该是低位在前,高位在后。
     

  • 相关阅读:
    HDU 5087 (线性DP+次大LIS)
    POJ 1064 (二分)
    Codeforces 176B (线性DP+字符串)
    POJ 3352 (边双连通分量)
    Codeforces 55D (数位DP+离散化+数论)
    POJ 2117 (割点+连通分量)
    POJ 1523 (割点+连通分量)
    POJ 3661 (线性DP)
    POJ 2955 (区间DP)
    LightOJ 1422 (区间DP)
  • 原文地址:https://www.cnblogs.com/gpwzw/p/12147507.html
Copyright © 2011-2022 走看看