- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Number floatValue() Method
The Java Number floatValue() method is used to convert the value of the specified number into a float value.
A float is a primitive data type that is a single-precision 32-bit IEEE 754 floating point value. The range of a float value starts from 3.40282347 x 1038 to 1.40239846 x 10-45. This data type is used when there is a need to save memory while storing large amount of floating-point arrays.
The default value of float is 0.0f; and its default size is 4 bytes.
Note − This conversion may involve rounding or truncation of the given number.
Syntax
Following is the syntax for Java Number floatValue() method
public abstract float floatValue()
Parameters
This method does not accept any parameters.
Return Value
This method returns the float value after converting the given numeric value.
Example
The following example shows the usage of Java Number floatValue() method. An integer value is assigned to the Integer class reference variable; and the method is invoked on this object created to convert the integer value to a float value.
public class NumberFloatVal { public static void main(String[] args) { // get a number as integer Integer x = 123456; // print their value as float System.out.println("x as integer: " + x + ", x as float: " + x.floatValue()); } }
Let us compile and run the above program, this will produce the following result −
x as integer: 123456, x as float: 123456.0
Example
In this example, we first assign a long value to a Long class reference variable. This method is then invoked on the Float object created and converts the long value to the float value.
public class NumberFloatVal { public static void main(String[] args) { // Get a number as long Long x = (long) 192873; // Print their value as float System.out.println("x as long: " + x + ", x as float: " + x.floatValue()); } }
After compiling and executing the program, the output is displayed as −
x as long: 192873, x as float: 192873.0
Example
Here, similar to the previous example, a Double class reference variable has a double value assigned to it. The method is then invoked on this object to convert the double value into its corresponding float value.
public class NumberFloatVal { public static void main(String[] args) { // get a number as double Double x = 92873d; // print their value as float System.out.println("x as double: " + x + ", x as float: " + x.floatValue()); } }
The output for the program above is given as follows −
x as double: 92873.0, x as float: 92873.0
Example
We can also convert a byte value into its corresponding double value. A byte value is assigned to the Byte class reference variable after which an object is created. The method will be invoked on this object to convert the byte value into a double value.
public class NumberFloatVal { public static void main(String[] args) { // get a number as byte Byte x = -120; // print their value as float System.out.println("x as byte: " + x + ", x as float: " + x.floatValue()); } }
Once the program is compiled and run, the output is displayed as follows −
x as byte: -120, x as float: -120.0
To Continue Learning Please Login
Login with Google