Convert very small double to string

I have a very small number, and I want to convert it to String with the full number, not abbreviated. I do not know how small this number is.

for example, at startup:

double d = 1E-10;
System.out.println(d);

it shows 1.0E-10instead of 0.000000001.

I already tried NumberFormat.getNumberInstance(), but it formats 0. and I don’t know which expression to use in DecimalFormatto work with any number.

+5
source share
4 answers

Assuming you want 500 zeros in front of your number when you do:

double d = 1E-500;

then you can use:

double d = 1E-10;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(Integer.MAX_VALUE);
System.out.println(nf.format(d));
+10
source

setMinimumFractionDigits setMaximumFractionDigits. .

+3

You can do this with BigDecimals in Java 5 using:

System.out.println(new java.math.BigDecimal(Double.toString(1E-10)).stripTrailingZeros().toPlainString());

Note that if you have a double value as a string in the first place, you would be better off using:

System.out.println(new java.math.BigDecimal("1E-10").toPlainString());

... as described in BigDecimal javadocs.

+2
source
-2
source

All Articles