Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 862

Converting double to string - Java Program

$
0
0

In the post converting String to double we have already seen ways to do that conversion. This post is about doing just the reverse; convert double values to string.

Concatenating with an empty String

Easiest way to convert double to a string is to concatenate double with an empty string. That will give you a string value, conversion is handled for you.


public class DoubleToString {

public static void main(String[] args) {
double num = 78.111167d;
String str = "" + num;
System.out.println("Value " + str);
}
}

Output


Value 78.111167

Here note that with double value you can use d or D (even f or F, for single float). If you use f instead of d value may be a little different.

Using valueOf() method of String class

String class has valueOf() method which is overloaded and those variants take int, float, double, long data types as parameters. Using valueOf(double d) method will return string representation of the double argument.


public class DoubleToString {
public static void main(String[] args) {
double num = -67.16789;
String str = String.valueOf(num);
System.out.println("Value " + str);

}
}

Output


Value -67.16789

toString() method of the wrapper classes

Each of the Number subclass (Integer, Float, Double etc.) includes a class method, toString(), that will convert its primitive type to a string. Thus, using Double.toString(double d) returns a String object representing the specified float value.


public class DoubleToString {

public static void main(String[] args) {
double num = 124686.9698694d;
String str = String.valueOf(num);
System.out.println("Value " + str);

}
}

Output


Value 124686.9698694

Using String.format method

  • String format(String format, Object... args) - Returns a formatted string using the specified format string and arguments.
Here as a format you can use 'f' which means floating point and the result is formatted as a decimal number.

Example code


public class DoubleToString {

public static void main(String[] args) {
double num = 124686.9698694d;
String str = String.format("%.2f", num);
System.out.println("Value " + str);
}
}

Output


Value 124686.97

Here note that .2f is used as format so there will be 2 decimal places. In the syntax you can see that second argument is a vararg which is of type Object. Still you can pass double primitive data type because of autoboxing.

That's all for this topic Converting double to string - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Converting float to string - Java Program
  2. Displaying prime numbers - Java program
  3. How to format date in Java using SimpleDateFormat
  4. How to find the longest palindrome in the given String
  5. Java String interview questions

You may also like -

>>>Go to Java Programs page


Viewing all articles
Browse latest Browse all 862

Trending Articles