java (beginner) converting scientific notation to

2019-06-16 02:00发布

if

double d =  1.999e-4

I want my output to be 0.0001999.

How can I do it?

5条回答
Evening l夕情丶
2楼-- · 2019-06-16 02:09

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-16 02:11

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.

查看更多
Lonely孤独者°
4楼-- · 2019-06-16 02:21

Take a look over

java.text.DecimalFormat

and

java.text.DecimalFormatSymbols
查看更多
啃猪蹄的小仙女
5楼-- · 2019-06-16 02:31

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.

查看更多
别忘想泡老子
6楼-- · 2019-06-16 02:36
NumberFormat formatter = new DecimalFormat("###.#####");  

String f = formatter.format(d);  

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

查看更多
登录 后发表回答