How to convert a string 3.0103E-7 to 0.00000030103 in Java?

回眸只為那壹抹淺笑 提交于 2019-12-22 13:50:31

问题


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.


回答1:


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.




回答2:


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 bg = new BigDecimal(rs.getString(i));
Formatter fmt = new Formatter();
fmt.format("%." + bg.scale() + "f", bg);
buf.append( fmt);



回答3:


Using 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



回答4:


double d = Double.parseDouble("7.399999999999985E-5");
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
System.out.println(f);    // output --> 0.00007



回答5:


I haven't tried it, but java.text.NumberFormat might do what you want.



来源:https://stackoverflow.com/questions/1229516/how-to-convert-a-string-3-0103e-7-to-0-00000030103-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!