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

送分小仙女□ 提交于 2019-12-06 15:14:29

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.

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);

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
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

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

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