Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert very small double to a String

I have a very small number and I want to convert it to a String with the full number, not abbreviated in any way. I don't know how small this number can be.

for example, when I run:

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

it shows 1.0E-10 instead of 0.000000001.

I've already tried NumberFormat.getNumberInstance() but it formats to 0. and I don't know what expression to use on a DecimalFormat to work with any number.

like image 493
cd1 Avatar asked Jan 20 '10 18:01

cd1


People also ask

Can we convert double to String in java?

Using the “+” operator − The + operator is an addition operator but when used with Strings it acts as a concatenation operator. It concatenates the other operand to String and returns a String object. You can convert a double value into a String by simply adding it to an empty String using the “+” operator.


1 Answers

Assuming that you want 500 zeroes 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));
like image 120
ryanprayogo Avatar answered Sep 17 '22 06:09

ryanprayogo