Open In App

Java Number.floatValue() Method

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The floatValue() method is an inbuilt method in Java from the java.lang.Number class. This method is used when we want to extract the value of a Number object as a float data type.

This may involve rounding or truncation, because this method returns the numeric value of the current object converted to float type. The float data type has limited precision that is 32-bit, so, the method may truncate or round the value when converting from a higher precise type like double or long.

Syntax of floatValue() Method

public abstract float floatValue();

  • Parameters: This method does not accept any parameters. 
  • Returns: This method returns the numeric value represented by this object after conversion to type float.

This method is very helpful when we work with mixed data types and we want floating-point output from different numeric wrapper classes.

Examples of Java Number.floatValue() Method

Example 1: In this example, we are going to convert Integer and Double to float.

Java
// Java program to demonstrate 
// floatValue() with Integer and Double
import java.lang.Number;

public class Geeks {

    public static void main(String[] args) {

        // Integer value
        Integer n1 = new Integer(1785423456);
        float floatFromInt = n1.floatValue();
        System.out.println("Integer to float: " 
        + floatFromInt);

        // Double value
        Double n2 = new Double(97854876);
        float floatFromDouble = n2.floatValue();
        System.out.println("Double to float: " 
        + floatFromDouble);
    }
}

Output
Integer to float: 1.78542349E9
Double to float: 9.785488E7


Example 2: In this example, we are going to take smaller values with Integer and Double.

Java
// Java program to demonstrate 
// floatValue() with smaller values
import java.lang.Number;

public class Geeks {

    public static void main(String[] args) {

        // Integer value
        Integer n1 = new Integer(456);
        System.out.println("Integer to float: " 
        + n1.floatValue());

        // Double value
        Double n2 = new Double(96);
        System.out.println("Double to float: " 
        + n2.floatValue());
    }
}

Output
Integer to float: 456.0
Double to float: 96.0

Important Points:

  • We should be more cautious while using this method because there is a chance of loss of precision.
  • This might happen because we are converting large values or values with high decimal precision.

Practice Tags :

Similar Reads