LAST UPDATED: OCTOBER 15, 2020
Java Double toString(double d) Method
Java toString()
method is a part of the Double
class of the java.lang
package. This method returns the equivalent String object of the double value passed as an argument.
In short, this method is used to convert a double value into String.
Syntax:
public static String toString(double d)
Parameter:
The parameter passed is the double value whose equivalent String is to be returned.
Returns:
Returns the String representation of the double argument passed as a parameter. It must be noted that String "NaN" is returned for the double value, NaN.
Example 1:
Here, the String representations of the double arguments passed are returned.
import java.lang.Double;
public class StudyTonight
{
public static void main(String[] args)
{
double a = 40.95;
double b = -56.27;
System.out.println("Equivalent String is : "+Double.toString(a));
System.out.println("Equivalent String is : "+Double.toString(b));
}
}
Equivalent String is : 40.95
Equivalent String is : -56.27
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value ");
Scanner sc = new Scanner(System.in);
double val = sc.nextDouble();
String s = Double.toString(val); //converting to string
System.out.println("String value is : "+ s);
}
catch(Exception e)
{
System.out.println("Invalid input!!");
}
}
}
Enter the value NaN
String value is : NaN
*****************************
Enter the value 34.88
String value is : 34.88
*****************************
Enter the value 0x699
Invalid input!!
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.