Skip to main content

Double to string conversion in Java

Double to String conversion

Two ways to convert double to String:

1: Using valueOf() method

public static String valueOf(double d)
We can convert the double primitive to String by calling valueOf() method of String class. This method returns the string representation of the double argument.
double var = 22.22;
String strg = String.valueOf(var);

2: Using toString() method

public String toString( ):
This is the another method that can be used to convert double to String. This method returns a string representation of the Double object. The primitive double value represented by this object is converted to a string.
double var = 33.33;
String str = Double.toString(var);

Example:



public class DoubleToString {
   public static void main(String[] args) {
        
       // Method 1: using valueOf() method of String class
        
       double dvar = 201.11;
       String strg = String.valueOf(dvar);
       System.out.println("String is: "+strg);
        
       // Method 2: using toString() method of Double class
   
       double dvar1 = 200.202;
       String strg1 = Double.toString(dvar1);
       System.out.println("String2 is: "+strg1);
   }
}
Output:
String is: 201.11
String2 is: 200.202

Comments

  1. Wow, very nice blog. really it is very interesting and helpful. Please keep sharing such type of information. Thanks!

    ReplyDelete
  2. Often we need to convert a double to a string in Java and there are many different ways to do it. The String.valueOf() method, the Double.toString() method or the String.format() method will all be a solid way to convert our numerical values into their string representation. For displaying data, logging information or doing some string manipulation this conversion is essential. If you are a business and you think of optimizing your Java application, partnering with an experienced web development company in new york will give you the knowledge to provide the best possible solutions to achieve a seamless and smooth running operation of your business.

    ReplyDelete

Post a Comment