在 JAVA 中将数字从科学记数法转换为十进制

Convert a number from scientific notation to decimal in JAVA

我有一个问题:如果一个数字在小数点前有 8 位或更多位,则它以科学计数法显示。
有没有一种简单的方法可以通过库或其他方式将此数字转换为十进制数?
我开始创建一个手动方法来解析它,但它似乎过于复杂。

任何帮助将不胜感激。

input example: 1.0225556677556E7

编辑:我还需要能够识别科学记数法中的数字

您可以使用 NumberFormat 轻松实现您的目标。

String scientificNotation = "1.0225556677556E7";
Double scientificDouble = Double.parseDouble(scientificNotation);
NumberFormat nf = new DecimalFormat("################################################.###########################################");
decimalString = nf.format(scientificDouble);

要回答您关于匹配科学记数法字符串的其他问题 - 您可以使用正则表达式和 String.matches()。这个并不完美,虽然得到误报的概率应该很低:

if(myString.matches("-?[\d.]+(?:E-?\d+)?")){
    //do work
}