Open In App

Java Number.doubleValue() Method

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

The Number.doubleValue() is an inbuilt method in Java from java.lang.Number package. This method returns the value of the specified number cast as a double data type. This may involve rounding or truncation.

Syntax of doubleValue() Method

public abstract double doubleValue()

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

This method is mostly used when we need to perform arithmetic operations that require higher precision. The double data type can hold fractional values, so, it is useful for converting integer-based values when working with mixed-type arithmetic expressions.

Example of Java Number.doubleValue() Method

Example 1: In this example, we are going to convert Integer and Float into double using Number.doubleValue() method.

Java
// Java program to demonstrate the 
// use of doubleValue() method
import java.lang.Number;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create an Integer object
        Integer i = Integer.valueOf(123456);

        // Convert Integer to double
        double doubleFromInt = i.doubleValue();
        System.out.println("Integer as double: " + doubleFromInt);

        // Create a Float object
        Float f = Float.valueOf(9876.54f);

        // Convert Float to double
        double doubleFromFloat = f.doubleValue();
        System.out.println("Float as double: " + doubleFromFloat);
    }
}

Output
Integer as double: 123456.0
Float as double: 9876.5400390625

Note: When we are converting Float to double, there might be a small precision difference due to internal representation of floating-point numbers.


Example 2: In this example, we are going to use doubleValue() with smaller values.

Java
// Java program to demonstrate the 
// use of doubleValue() method
import java.lang.Number;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Create an Integer object with a small value
        Integer i = Integer.valueOf(123);

        // Convert Integer to double
        double doubleFromInt = i.doubleValue();
        System.out.println("Integer as double: " + doubleFromInt);

        // Create a Float object with a small value
        Float f = Float.valueOf(9876f);

        // Convert Float to double
        double doubleFromFloat = f.doubleValue();
        System.out.println("Float as double: " + doubleFromFloat);
    }
}

Output
Integer as double: 123.0
Float as double: 9876.0


Important Points:

  • The doubleValue() method is helpful when we want to convert different number types like Integer, Float, or Long, into a double.
  • We do not have to write extra code to cast them manually. Just by calling the method, it give us the double version of the number.
  • This method is really very helpful when we are doing calculations that need decimal points, like measurements or currency values.
  • It also makes our code cleaner if we are working with different types of numbers and want them all in a consistent format.

Practice Tags :

Similar Reads