如何在 Java 中将字符串 3.0103E-7 转换为 0.00000030103?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1229516/
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
How to convert a string 3.0103E-7 to 0.00000030103 in Java?
提问by erotsppa
How to convert a string 0E-11 to 0.00000000000 in Java? I want to display the number in non scientific notations. I've tried looking at the number formatter in Java, however I need to specific the exact number of decimals I want but I will not always know. I simply want the number of decimal places as specificed by my original number.
如何在 Java 中将字符串 0E-11 转换为 0.00000000000?我想以非科学记数法显示数字。我试过查看 Java 中的数字格式化程序,但是我需要指定我想要的确切小数位数,但我并不总是知道。我只想要原始数字指定的小数位数。
回答by erotsppa
Apparently the correct answer is to user BigDecimal and retrieve the precision and scale numbers. Then use those numbers in the Formatter. Something similar like this:
显然,正确的答案是使用 BigDecimal 并检索精度和小数位数。然后在格式化程序中使用这些数字。类似的东西:
BigDecimal bg = new BigDecimal(rs.getString(i));
Formatter fmt = new Formatter();
fmt.format("%." + bg.scale() + "f", bg);
buf.append( fmt);
回答by BullyWiiPlaza
Using BigDecimal:
使用BigDecimal:
public static String removeScientificNotation(String value)
{
return new BigDecimal(value).toPlainString();
}
public static void main(String[] arguments) throws Exception
{
System.out.println(removeScientificNotation("3.0103E-7"));
}
Prints:
印刷:
0.00000030103
回答by Eugene Ryzhikov
I would use BigDecimal.Pass your string into it as a parameter and then use String.format to represent your newly created BigDecimal without scientific notation. Float or Double classes can be used too.
我会使用 BigDecimal.Pass 你的字符串作为参数,然后使用 String.format 来表示你新创建的 BigDecimal 没有科学记数法。也可以使用 Float 或 Double 类。
回答by Alysson Fonseca
double d = Double.parseDouble("7.399999999999985E-5");
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
System.out.println(f); // output --> 0.00007
回答by Powerlord
I haven't tried it, but java.text.NumberFormatmight do what you want.
我还没有尝试过,但java.text.NumberFormat可能会做你想做的。

