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.01The 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-5The problem with this kind of conversion is that it is hard for humans to read.
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.000012345678The 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
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.0000000000Which is pretty cool.