java.lang.Number类是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long和Short类的超类。Number的子类必须提供方法将表示的数字值转换为Byte、Double、Float、Integer、Long和Short。
该方法以字节形式返回指定数字的值,这可能涉及到四舍五入或截断。
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
。
该方法将指定数字的值作为一个双数返回,可能涉及到四舍五入。
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
该方法将指定数字的值作为浮点数返回,这可能涉及到四舍五入。
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
该方法将指定数字的值作为一个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
该方法将指定的数字的值作为长数返回,这可能涉及到四舍五入或截断。
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
该方法将指定数字的值作为短值返回,这可能涉及到四舍五入或截断。
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
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/12/java-number-class-methods-with-examples.html
内容来源于网络,如有侵权,请联系作者删除!