Signup/Sign In
LAST UPDATED: OCTOBER 15, 2020

Java Double toString() Method

Java toString() method is a part of the Double class of the java.lang package. This method returns the String object with its value equivalent to the Double Object.

In short, this method is used to convert Double Object into String.

Syntax:

public String toString()  

Parameters:

No parameters are passed in this method.

Returns:

Returns the String representation of the Double Object.

Example 1:

Here, the Double objects are converted into its equivalent String representations.

import java.lang.Double;
public class StudyTonight
{  
    public static void main(String[] args) 
      {  
        Double a = 50.67;       
        String s1 = a.toString();  // return a string value 
        System.out.println("Equivalent String is " + s1);              
        
        Double b = -18.80;            
        String s2 = b.toString();  // return a string value 
        System.out.println("Equivalent String is " + s2);  
      }  
}  


Equivalent String is 50.67
Equivalent String is -18.8

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 = val.toString(); //converting to string
             System.out.println("String value is : "+ s);  
         
           }
         catch(Exception e)
           {
           System.out.println("Invalid input!!");
           }     
        }  
}  


Enter the value 89.55
String value is : 89.55
***************************
Enter the value -44.89
String value is : -44.89
***************************
Enter the value 0x556
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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.