Java - Number doubleValue() Method



The Java Number doubleValue() method is used to convert the value of any number to its corresponding a double value.

A double is a primitive data type that is a 64-bit double precision floating-point value with a range greater than the float data type according to the IEEE 754 standards. It ranges from values 1.7976931348623157 x 10308 to 4.9406564584124654 x 10-324 (both positive and negative).

The default value and size of double is 0.0d and 8 bytes respectively.

Note − This conversion may involve rounding or truncation of the given number.

Syntax

Following is the syntax for Java Number doubleValue() method

public abstract double doubleValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the double value after conversion.

Example

The following example shows the usage of Java Number doubleValue() method. Here, we are instantiating an Integer object and initializing it. The method is then invoked on this object. The return value obtained will be the corresponding double value.

public class NumberDoubleVal {
   public static void main(String[] args) {
      // get a number as integer
      Integer x = new Integer(123456);

      // print their value as double
      System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
   }
}

Let us compile and run the above program, this will produce the following result −

x as integer :123456, x as double:123456.0

Example

Another way to implement the example above is seen as follows. An object is created after assigning an integer value to the Integer class reference variable. The method is then invoked on this object to obtain the double value.

public class NumberDoubleVal {
   public static void main(String[] args) {

      // get a number as integer
      Integer x = 123456;

      // print their value as double
      System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
   }
}

Let us compile and run the above program, this will produce the following result −

x as integer :123456, x as double:123456.0

Example

Like the previous example, a Long object is created here and the method is called on it to convert it into a double value.

public class NumberDoubleVal {
   public static void main(String[] args) {

      // get a number as long
      Long x = (long) 8731029;
 
      // print their value as double
      System.out.println("x as long: " + x + ", x as double: " + x.doubleValue());
   }  
}

The output for the program above is given as follows −

x as long: 8731029, x as double: 8731029.0

Example

The following example creates a Float object and invokes the method on this object. The method then converts the value assigned to this object into a double value.

public class NumberDoubleVal {
   public static void main(String[] args) {

      // get a number as float
      Float x = 67929f;
  
      // print their value as double
      System.out.println("x as float: " + x + ", x as double: " + x.doubleValue());
   }  
}

On compiling and executing the given program, the output is displayed as follows −

x as float: 67929.0, x as double: 67929.0

Example

In here, the method invoked on a Byte object, converting the assigned byte value to a double value.

public class NumberDoubleVal {
   public static void main(String[] args) {

      // get a number as byte
      Byte x = 110;
  
      // print their value as double
      System.out.println("x as byte: " + x + ", x as double: " + x.doubleValue());
   }  
}

Once we compile and run the program, the output is printed as given below −

x as byte: 110, x as double: 110.0
java_lang_number.htm
Advertisements