## Java Double To String Without Exponential Scientific Notation

When dealing with numbers that are not whole, the type double is usually used for convenience because it is primitive and has good precision. However, when a double variable is converted to String, most of the time the result is in exponential or scientific notation. Below are some examples on how to perform in Java the conversion from double to String without exponential or scientific notation.

### Problem Scenario

When we are trying to convert a double value to String with few digits, the result usually does not use exponential or scientific notation. For example:
```public class Test {
public static void main(String[] args) {
double firstNumber = 12;
double secondNumber = 0.01;
String firstNumberAsString = String.valueOf(firstNumber);
String secondNumberAsString = String.valueOf(secondNumber);
System.out.println(firstNumberAsString);
System.out.println(secondNumberAsString);
}
}
```
Will display the result below, which has no problems at all:
```12.0
0.01
```
The problem will occur if the number we are converting has a lot of digits, or requires higher precision. Consider the example code 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);
}
}
```

The code will have an output with scientific notation, as shown below:

```1.2345678E7
1.2345678E-5
```

The problem with this kind of conversion is that it is hard for humans to read.

### String.format() Solution

One solution to avoid exponential notation when converting Double to String is to use String.format(). Here is a simple example:
```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);
}
}
```

The "%.0f" parameter tells the method that we want 0 decimal places, which means we only want the whole number. The "%.12f" parameter means we wish 12 decimal places. The code outputs:

```12345678
0.000012345678
```

The example assumes we have some knowledge on how many digits are to the left and to the right of the decimal point. We may use the same number of digits for both the whole number and the decimal part. Here is an example.
```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);
}
}
```

And the output will be:

```12345678.0000000000
0.0000123457
```

### DecimalFormat Solution

Another way to control the output format and avoid the scientific notation in double to String conversion is by using DecimalFormat. Here is a simple example:
```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 output of this code is:

```12345678.0000000000
0.0000123457
```

Aside from removing the scientific or exponential notation, there are some other controls. Here is a modified sample that uses 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);
}
}
```
Aside from avoiding scientific notation, the result will also have commas for every three digit whole numbers.
```12,345,678.0000000000
```

Which is pretty cool.