Java中的浮点 floatValue() 和示例
Float 类中的floatValue()方法是Java中的内置函数,它在类型转换后将调用对象指定的值返回为 float。
语法:
public float floatValue()
返回值:它将 FloatObject 的值返回为float 。
下面的程序说明了Java中的floatValue()方法:
方案一:
Java
// Java code to demonstrate
// Float floatValue() method
class GFG {
public static void main(String[] args)
{
// Float value
Float a = 17.47f;
// wrapping the Float value
// in the wrapper class Float
Float b = new Float(a);
// floatValue of the Float Object
float output = b.floatValue();
// prfloating the output
System.out.println("Float value of "
+ b + " is : " + output);
}
}
Java
// Java code to demonstrate
// Float floatValue() method
class GFG {
public static void main(String[] args)
{
String value = "54.1";
// wrapping the Float value
// in the wrapper class Float
Float b = new Float(value);
// floatValue of the Float Object
float output = b.floatValue();
// prfloating the output
System.out.println("Float value of "
+ b + " is : " + output);
}
}
输出:
Float value of 17.47 is : 17.47
方案二:
Java
// Java code to demonstrate
// Float floatValue() method
class GFG {
public static void main(String[] args)
{
String value = "54.1";
// wrapping the Float value
// in the wrapper class Float
Float b = new Float(value);
// floatValue of the Float Object
float output = b.floatValue();
// prfloating the output
System.out.println("Float value of "
+ b + " is : " + output);
}
}
输出:
Float value of 54.1 is : 54.1
参考: https: Java/lang/Float.html#floatValue()