Grails Cookbook - A collection of tutorials and examples

Double To String Without Exponential Scientific Notation in Java

What we will discuss in this topic is another data type which is called double. A good advantage from this data type is its conveniency and precision when it comes to dealing with numbers that are not whole. Although sometimes, when we try to convert a Double variable to a String, we get answers that are in exponential or scientific notation form. Check out below some examples of the process of converting Java double to string without exponential scientific notation in the Java Programming Language.

Issue

Let's try to convert a double value to String with just a few digits. Check out the written example below:
public class Test {
    public static void main(String[] args) {
        double firstNumber = 0.02;
        String firstNumberAsString = String.valueOf(firstNumber);
        System.out.println("Converting double first number to String: " + firstNumberAsString);
    }
}
This is the result of the code written in the problem scenario. The output displayed neither in exponential nor scientific notation form.
Converting double first number to String: 0.02
Below is where the issue or conflict occurs, when we try to convert a number with a lot of digits or when we need higher precision. Observe the written example below:
public class Test {
    public static void main(String[] args) {
        double firstNumber = 12345678;
        double secondNumber = 0.000012345678;
        String firstNumberAsString = String.valueOf(firstNumber);
        String secondNumberAsString = String.valueOf(secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}
1.2345678E7
1.2345678E-5
What we did is we tried to convert a large number of digits. Notice that the output shown is in scientific notation form. Understand that this conversion is difficult to read and takes an effort for humans. This is not good if our target audience are humans for the output.

Solution using DecimalFormat.format()

The DecimalFormat solution is another way to control the output format. We can also avoid having exponential and scientific notation in our expected results. Check out the written example below:
import java.text.DecimalFormat;
public class Test {
    public static void main(String[] args) {
        double firstNumber = 12345678;
        double secondNumber = 0.000012345678;
        DecimalFormat decimalFormat = new DecimalFormat("0.0000000000");
        String firstNumberAsString = decimalFormat.format(firstNumber);
        String secondNumberAsString = decimalFormat.format(secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}
The result is shown below:
12345678.0000000000
0.0000123457

This is a modified sample when you want to use a thousand separator:
import java.text.DecimalFormat;
public class Test {
    public static void main(String[] args) {
        double firstNumber = 12345678;
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.0000000000");
        String firstNumberAsString = decimalFormat.format(firstNumber);
        System.out.println(firstNumberAsString);
    }
}

What the code displays is the commas for every three digit whole numbers.
12,345,678.0000000000
Through these parameters, conversion of double to string without exponential and scientific notation is made easy, which is pretty much amazing.

Solution using String.format()

For every conflict, there's a corresponding solution. There is a code we can use to evade these results. What we want to use is String.format() when it comes to converting double to string so that we will not acquire outputs with exponential and scientific notation. Check out the simple example below:
public class Test {
    public static void main(String[] args) {
        double firstNumber = 12345678;
        double secondNumber = 0.000012345678;
        String firstNumberAsString = String.format ("%.0f", firstNumber);
        String secondNumberAsString = String.format("%.12f",secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}
We like the first number as a whole number with 0 decimal places and the purpose of the "%.Of" parameter expresses the method that we don't want it to be neither in exponential nor scientific notation. And as for the second number, "%.12f" parameter will display the output with 12 decimal places.

Check out the output below:

12345678
0.000012345678
Understand that the example acknowledges that we know how many digits there are in both sides of the decimal point. Now, observe what happens in the written example below:
public class Test {
    public static void main(String[] args) {
        double firstNumber = 12345678;
        double secondNumber = 0.000012345678;
        String firstNumberAsString = String.format ("%.10f", firstNumber);
        String secondNumberAsString = String.format("%.10f",secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}

The result is shown below:

12345678.0000000000
0.0000123457

The first number displayed 10 decimal places, so did the second number but notice what happens to the rest of the decimal places written in the variable secondNumber. Since we used "%.10f", the parameter only showed through the 10th value which is the 6 number. And because 6 can be rounded, the result showed you: 0.0000123457.