Grails Cookbook - A collection of tutorials and examples

Double To String With 2 Decimal Places In Java

We will now discuss how to convert Java Double To String using 2 Decimal Places, and what are the methods and parameters that can help you acquire it. Although with that knowledge, an integer becomes an number if there's a separator that can describe what's it for. For example, 1234 is 12.34 if we put a dollar sign and separate it with a decimal point. Another example is when we put a comma separator to make it $1,234.00. In this topic, we will explore the details and process of converting a double to string with 2 decimal places. Check out the written examples below.

DecimalFormat Solution

One class can also format a number to a String subsequent a given pattern, and that is the java.text.DecimalFormat class. Below is an example on how to convert a double into string using two decimal places with the help of DecimalFormat class:
/**
 * This is a simple Java example that converts a double to String using two decimal places through DecimalFormat.
 */
public class DoubleToStringTest {
    public static void main(String[] args) {
        double number = 555.7891d;
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String numberAsString = decimalFormat.format(number);
        System.out.println(numberAsString);
    }
}
The "#" parameter shows the whole number, while the ".00" notifies the formatter about the two decimal places. The output shows the rounded number. Check the details below:
555.79
We can also utilize the DecimalFormat as a thousand separator, let's observe the process in the given example:
/**
 * This is a simple Java example that converts a double to String using two decimal places and using comma 
 * as thousand separator through DecimalFormat.
 */
public class DoubleToStringTest {
    public static void main(String[] args) {
        double number = 12345678.7891d;
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        String numberAsString = decimalFormat.format(number);
        System.out.println(numberAsString);
    }
}
Not only we've converted the double variable number into a String, we also separated numbers with commas for every three digits. Let's not forget our rounded decimal places remained also.
Check out the output below:
12,345,678.79
These are the important methods and parameters in converting double to a String using 2 decimal places.

String.format() Solution

In Java 5 or higher versions, String.format() method has the capibility to convert double to a String value. We will apply the two decimal places for the said conversion. Check out the process below.
/**
 * This is a simple Java example that converts a double to String using two decimal places through String.format().
 */
public class DoubleToStringTest {
    public static void main(String[] args) {
        double number = 333.4567d;
        String numberAsString = String.format ("%.2f", number);
        System.out.println("This is the converted number: " + numberAsString);
    }
}
What the parameter "%.2f" does is notify the formatter to utilize exactly two decimal places. Since our variable number has more than 2 decimal places, the parameter will only acquire .45, and when we round of this number, our output will be .46, check out the result below:
This is the converted number: "333.46
Let's now try the String.format with thousand indicator using comma and observe the written example below:
/**
 * This is a simple Java example that converts a double to String using two decimal places and using comma 
 * as thousand separator through String.format().
 */
public class DoubleToStringTest {
    public static void main(String[] args) {
        double number = 1098765432.4567d;
        String numberAsString = String.format ("%,.2f", number);
        System.out.println(numberAsString);
    }
}
The parameter "%,.2f" notifies us that the thousand separator is a comma, and the two decimal digit will retain once it is converted. Therefore for every 3 digits from the right, the whole number is now separated with a comma. And the two decimal places that has been rounded remained.

Check out the output shown below:

1,098,765,432.46