In this tutorial, you will learn how to convert a Double to a String in Java. The double data type is one of the primitive types in Java that you can use when working with numerals.
When working with decimals, this is the recommended type to go for. Despite this, the data type is not used for precise values such as currency. It is a good practice to use BigDecimal
when working with money.
Let’s look at an example to understand how this works.
Using type coercion
After creating a Java project, Intellij creates a file named Main.java which has the main method. Copy and paste the following code into the file.
package com.javawhizz; public class Main { public static void main(String[] args) { double price = 1200.36; String description = "Total price = " + price; System.out.println(description); } }
The variable named price defines a double variable that has a value of 1200.36
. To use type coercion to convert the double value to a string, you can use String concatenation.
To achieve this, create a String and use the addition sign to concatenate it with the double variable. Behind the scenes, Java will perform type coercion to convert the double to string.
Output
Total price = 1200.36
Using Double.toString()
Copy and paste the following code into the file named Main.java.
package com.javawhizz; public class Main { public static void main(String[] args) { double price = 1200.36; String doubleToString = "Total price = " + Double.toString(price); System.out.println(doubleToString); } }
The Double.toString()
method returns a String representation of the argument. Since the toString()
method is a static method of the Double
wrapper class, you must pass a double value to it.
Output
Total price = 1200.36
Using String.valueOf()
Copy and paste the following code into the Main.java file.
package com.javawhizz; public class Main { public static void main(String[] args) { double price = 1200.36; String stringValueOf = "Total price = " + String.valueOf(price); System.out.println(stringValueOf); } }
The String.valueOf()
method returns a String representation of the double value. In fact, the value returned is exactly the same as the one returned by the Double.toString()
method.
Output
Total price = 1200.36
Using String.format()
Copy and paste the following code into the Main.java file.
package com.javawhizz; public class Main { public static void main(String[] args) { double price = 1200.36; String stringFormat = "Total price = " + String.format("%s", price); System.out.println(stringFormat); } }
The String.format()
method accepts two arguments to return a formatted String. The first argument is for the format string and the second argument is for the list of arguments.
Java provides different formats that you can use to format your output. There are formats for every data type in Java including numeric, string, and date/time.
The format string %s
formats the double value passed as a String. The second parameter expects the double argument.
Output
Total price = 1200.36
Using DecimalFormat
Copy and paste the following code into the Main.java file.
package com.javawhizz; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double price = 1200.36; DecimalFormat decimalFormat = new DecimalFormat(); String decimalFormatLong = "Total price = " + decimalFormat.format(price); System.out.println(decimalFormatLong); } }
DecimalFormat
is a class that provides the functionality to parse and format decimals. This means that the class works with float and double data types.
To format a double to a String, create an instance of DecimalFormat
. Next, call the format()
method and then pass the double value. This will format the provided double value to a String.
Output
Total price = 1,200.36
Using StringBuilder and StringBuffer
Copy and paste the following code into the Main.java file.
package com.javawhizz; public class Main { public static void main(String[] args) { double price = 1200.36; StringBuilder stringBuilder = new StringBuilder(); String stringBuilderAppend = "Total price = " + stringBuilder.append(price); System.out.println(stringBuilderAppend); } }
The StringBuilder
provides the functionality to create a mutable sequence of characters. The main methods provided by this class are append()
and insert()
.
It also supports method overloading which allows you to work with any data type. The argument passed to any of the methods gets converted to a String and then added to the StringBuilder
.
The append()
method appends the String at the end of the current String. On the contrary, the insert()
method inserts a String at a specific position in the current String.
Output
Total price = 1200.36
Conclusion
In this tutorial, you have learned how to convert a Double to a String in Java. The approaches covered include using type coercion, Double.toString()
, String.valueOf()
, String.format()
, DecimalFormat
, StringBuilder
, and StringBuffer
.
Happy Hacking!
0 Comments