java(初学者)将科学记数法转换为十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13064567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java (beginner) converting scientific notation to decimal
提问by hibc
if
如果
double d = 1.999e-4
I want my output to be 0.0001999.
我希望我的输出为 0.0001999。
How can I do it?
我该怎么做?
采纳答案by RP-
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
You can explore the sub classes of NumberFormat
class to know more details.
您可以探索类的子类NumberFormat
以了解更多详细信息。
回答by Adam Arold
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.
查看NumberFormat
的方法的文档以double
根据您的需要设置格式。
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.
DecimalFormat
是一种特殊情况,NumberFormat
正如其构造函数所述,我认为您不需要它的功能。如果您感到困惑,请查看他们的文档。使用工厂方法getInstance()
的NumberFormat
为您提供方便。
回答by Harsh Chaturvedi
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 类中有一个方法叫做toPlainString()
. 例如,如果 BigDecimal 是 1.23e-8,则该方法返回 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.
上面的代码打印 0.0000000123,然后您可以根据您的要求处理字符串。
回答by specialscope
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.
您可以更改 10f, 10=您想要的小数位数。
回答by cmg_george
Take a look over
看一看
java.text.DecimalFormat
and
和
java.text.DecimalFormatSymbols