java.lang.Float doubleValue()
Description
Important Notes:
- specified by doubleValue in class Number
Method Syntax
public double doubleValue()
Method Returns
The doubleValue() method of Float class returns the the float value represented by this object converted to type double.
Compatibility
Java 1.2
Java Float doubleValue() Example
Below is a simple java example on the usage of doubleValue() method of Float class.
package com.javatutorialhq.java.examples; import java.util.Scanner; import static java.lang.System.*; /* * This example source code demonstrates the use of * doubleValue() method of Float class. */ public class FloatDoubleValueExample { public static void main(String[] args) { // Ask user input System.out.print("Enter Desired Value:"); // declare the scanner object Scanner scan = new Scanner(System.in); // use scanner to get value from user console Float userInput = scan.nextFloat(); // close the scanner object scan.close(); // convert the Float input to double double value = userInput.doubleValue(); out.println("Value in double is " + value); } }
Basically on the above example, we just ask for user input on the console and then we use the scanner object to get the float input. After that we assign the value to an Float wrapper class and then get the double equivalent using the doubleValue() method of Float class. Since widening happens on using this method, the result would not be the original value entered. It will include more decimal digits appended to the original number which is the result of the widening concept from float to double.
Sample Output
Below is the sample output when you run the above example. We have entered 3.12 on the console however additional digits are appended as a result of the widening behavior that happens during the conversion of float to double. This is an expected behavior of using doubleValue() method.