Java中的 Number.floatValue() 方法及示例
Java.lang.Number.floatValue()是Java中的一个内置方法,它返回转换为浮点数据类型的指定数字的值。这可能涉及舍入或截断。
句法:
public abstract float floatValue()
参数:此方法不接受任何参数。
返回:该方法返回此对象转换为float类型后所表示的数值。
下面的程序说明了 Number.floatValue() 方法:
方案一:
// Below program demonstrate the
// Number.floatValue() method
public class gfg {
public static void main(String[] args)
{
// number with integer datatype
Integer x = new Integer(1785423456);
// print the value as float
System.out.println(x.floatValue());
// number with double datatype
Double y = new Double(97854876);
// print the value as float
System.out.println(y.floatValue());
}
}
输出:
1.78542349E9
9.785488E7
方案二:
// Below program demonstrate the
// Number.floatValue() method
public class gfg {
public static void main(String[] args)
{
// number with integer datatype
Integer x = new Integer(456);
// print the value as float
System.out.println(x.floatValue());
// number with double datatype
Double y = new Double(96);
// print the value as float
System.out.println(y.floatValue());
}
}
输出:
456.0
96.0