Java Number类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(5.7k)|赞(0)|评价(0)|浏览(193)

java.lang.Number类是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long和Short类的超类。Number的子类必须提供方法将表示的数字值转换为Byte、Double、Float、Integer、Long和Short。

Java 数字类方法

  • byte byteValue() - 返回指定数字的字节值,可能涉及四舍五入或截断。
  • 抽象的doubleValue() - 返回指定数字的值,作为一个双数,可能涉及四舍五入。
  • 抽象的float floatValue() - 将指定的数字的值作为浮点数返回,这可能涉及到四舍五入。
  • abstract int intValue() - 将指定数字的值作为一个int返回,这可能涉及到四舍五入或截断。
  • 抽象的longValue() - 将指定的数字的值作为一个长数返回,这可能涉及到四舍五入或截断。
  • shortValue() - 返回指定数字的短值,这可能涉及到四舍五入或截断。

Java 数字类方法/API与实例

byte byteValue()

该方法以字节形式返回指定数字的值,这可能涉及到四舍五入或截断。

public void byteValueMethod() {
 // get a number as integer
 Number x = new Integer(123456);

 // get a number as float
 Number y = new Float(9876f);

 // print their value as byte
 System.out.println("x as integer:" + x + ", x as byte:" + x.byteValue());
 System.out.println("y as float:" + y + ", y as byte:" + y.byteValue());
}

输出:

x as integer:123456, x as byte:64
y as float:9876.0, y as byte:-108

抽象的doubleValue()

该方法将指定数字的值作为一个双数返回,可能涉及到四舍五入。

public void doubleValueMethod() {
 // get a number as integer
 Number x = new Integer(123456);

 // get a number as float
 Number y = new Float(9876f);

 // print their value as double
 System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
 System.out.println("y as float::" + y + ", y as double:" + y.doubleValue());
}

输出:

x as integer :123456, x as double:123456.0
y as float::9876.0, y as double:9876.0

abstract float floatValue()

该方法将指定数字的值作为浮点数返回,这可能涉及到四舍五入。

public void floatValueMethod() {
 // get a number as integer
 Number x = new Integer(123456);

 // get a number as double
 Number y = new Double(9876);

 // print their value as float
 System.out.println("x as integer:" + x + ", x as float:" + x.floatValue());
 System.out.println("y as double:" + y + ", y as float:" + y.floatValue());
}

输出:

x as integer:123456, x as float:123456.0
y as double:9876.0, y as float:9876.0

抽象的intValue()

该方法将指定数字的值作为一个int返回,这可能涉及到四舍五入或截断。

public void intValueMethod() {
 // get a number as float
 Number x = new Float(123456f);

 // get a number as double
 Number y = new Double(9876);

 // print their value as int
 System.out.println("x as float:" + x + ", x as Integer:" + x.intValue());
 System.out.println("y as double:" + y + ", y as Integer:" + y.intValue());
}

输出:

x as float:123456.0, x as Integer:123456
y as double:9876.0, y as Integer:9876

抽象的longValue()

该方法将指定的数字的值作为长数返回,这可能涉及到四舍五入或截断。

public void longValueMethod() {
 // get a number as float
 Number x = new Float(123456f);

 // get a number as double
 Number y = new Double(9876);

 // print their value as long
 System.out.println("x as float:" + x + ", x as long:" + x.longValue());
 System.out.println("y as double:" + y + ", y as long:" + y.longValue());
}

输出:

x as float:123456.0, x as long:123456
y as double:9876.0, y as long:9876

short shortValue()

该方法将指定数字的值作为短值返回,这可能涉及到四舍五入或截断。

public void shortValueMethod() {
 // get a number as float
 Number x = new Float(123456f);

 // get a number as double
 Number y = new Double(9876);

 // print their value as short
 System.out.println("x as float :" + x + ", x as short:" + x.shortValue());
 System.out.println("y as double:" + y + ", y as short:" + y.shortValue());
}

输出:

x as float :123456.0, x as short:-7616
y as double:9876.0, y as short:9876

###完整的例子

package net.javaguides.lang;

//*/*
/* Class demonstrates the usage of Random class methods with examples
/* 
/* @author Ramesh Fadatare
/*
/*/
public class NumberClassMethods {

    public static void main(String[] args) {
        NumberClassMethods classMethods = new NumberClassMethods();
        classMethods.shortValueMethod();
        classMethods.longValueMethod();
        classMethods.intValueMethod();
        classMethods.floatValueMethod();
        classMethods.doubleValueMethod();
        classMethods.byteValueMethod();
    }

    public void shortValueMethod() {
        // get a number as float
        Number x = new Float(123456 f);

        // get a number as double
        Number y = new Double(9876);

        // print their value as short
        System.out.println("x as float :" + x + ", x as short:" + x.shortValue());
        System.out.println("y as double:" + y + ", y as short:" + y.shortValue());
    }

    public void longValueMethod() {
        // get a number as float
        Number x = new Float(123456 f);

        // get a number as double
        Number y = new Double(9876);

        // print their value as long
        System.out.println("x as float:" + x + ", x as long:" + x.longValue());
        System.out.println("y as double:" + y + ", y as long:" + y.longValue());
    }

    public void intValueMethod() {
        // get a number as float
        Number x = new Float(123456 f);

        // get a number as double
        Number y = new Double(9876);

        // print their value as int
        System.out.println("x as float:" + x + ", x as Integer:" + x.intValue());
        System.out.println("y as double:" + y + ", y as Integer:" + y.intValue());
    }

    public void floatValueMethod() {
        // get a number as integer
        Number x = new Integer(123456);

        // get a number as double
        Number y = new Double(9876);

        // print their value as float
        System.out.println("x as integer:" + x + ", x as float:" + x.floatValue());
        System.out.println("y as double:" + y + ", y as float:" + y.floatValue());
    }

    public void doubleValueMethod() {
        // get a number as integer
        Number x = new Integer(123456);

        // get a number as float
        Number y = new Float(9876 f);

        // print their value as double
        System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
        System.out.println("y as float::" + y + ", y as double:" + y.doubleValue());
    }

    public void byteValueMethod() {
        // get a number as integer
        Number x = new Integer(123456);

        // get a number as float
        Number y = new Float(9876 f);

        // print their value as byte
        System.out.println("x as integer:" + x + ", x as byte:" + x.byteValue());
        System.out.println("y as float:" + y + ", y as byte:" + y.byteValue());
    }

}

输出。

x as float :123456.0, x as short:-7616
y as double:9876.0, y as short:9876
x as float:123456.0, x as long:123456
y as double:9876.0, y as long:9876
x as float:123456.0, x as Integer:123456
y as double:9876.0, y as Integer:9876
x as integer:123456, x as float:123456.0
y as double:9876.0, y as float:9876.0
x as integer :123456, x as double:123456.0
y as float::9876.0, y as double:9876.0
x as integer:123456, x as byte:64
y as float:9876.0, y as byte:-108

###参考文献

https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html

相关文章