Question

if

double d =  1.999e-4

I want my output to be 0.0001999.

How can I do it?

Was it helpful?

Solution

NumberFormat formatter = new DecimalFormat("###.#####");  

String f = formatter.format(d);  

You can explore the sub classes of NumberFormat class to know more details.

OTHER TIPS

You can do it like this:

    double d = 1.999e-4;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(7);
    System.out.println(nf.format(d));

Check out the documentation of NumberFormat's methods to format your double as you see fit.

DecimalFormat is a special case of NumberFormat as its constructor states, I don't think that you need its functionality for your case. Check out their documentation if you are confused. Use the factory method getInstance() of NumberFormat for your convenience.

I suppose there is a method in BigDecimal Class called toPlainString(). e.g. if the the BigDecimal is 1.23e-8 then the method returns 0.0000000124.

BigDecimal d = new BigDecimal("1.23E-8");

System.out.println(d.toPlainString());

Above code prints 0.0000000123, then you can process the string as per your requirement.

If all you want is to print like that.

System.out.printf("%1$.10f", d);

you can change 10f, 10=number of decimal places you want.

Take a look over

java.text.DecimalFormat

and

java.text.DecimalFormatSymbols
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top