Java 将科学记数法转换为十进制记数法

Java 将科学记数法转换为十进制记数法,java,numbers,scientific-notation,Java,Numbers,Scientific Notation,还有一个类似的问题,建议使用NumberFormat,这就是我所做的 我正在使用NumberFormat的parse()方法 public static void main(String[] args) throws ParseException{ DecToTime dtt = new DecToTime(); dtt.decToTime("1.930000000000E+02"); } public void decToTime(String angle) throws

还有一个类似的问题,建议使用NumberFormat,这就是我所做的

我正在使用NumberFormat的parse()方法

public static void main(String[] args) throws ParseException{

    DecToTime dtt = new DecToTime();
    dtt.decToTime("1.930000000000E+02");

}

public void decToTime(String angle) throws ParseException{

    DecimalFormat dform = new DecimalFormat();
    //ParsePosition pp = new ParsePosition(13);
    Number angleAsNumber = dform.parse(angle);

    System.out.println(angleAsNumber);
}
我得到的结果是

1.93


我真的没想到这会起作用,因为1.930000000000E+02是一个非常不寻常的数字,我需要先做一些字符串解析来删除零吗?或者有一种快速而优雅的方法吗?

当您在科学记数法中使用带表达式的十进制格式时,您需要指定一种模式。试试像这样的东西

DecimalFormat dform = new DecimalFormat("0.###E0");

请参阅--有一个标有“科学符号”的部分。

如果您将角度作为双精度,而不是字符串,则可以使用魔法

System.out.printf(“%.2f”,1.930000000000E+02)

显示小数点后两位的浮点值<代码>193.00

如果改用
“%.2e”
作为格式说明符,则会得到
“1.93e+02”


(不确定您想要什么样的输出,但可能会有帮助。)

记住
字符串。格式化
语法,这样您就可以将双精度和大小数转换为任何精度的字符串,而无需e符号:

此java代码:

double dennis = 0.00000008880000d;
System.out.println(dennis);
System.out.println(String.format("%.7f", dennis));
System.out.println(String.format("%.9f", new BigDecimal(dennis)));
System.out.println(String.format("%.19f", new BigDecimal(dennis)));
印刷品:

8.88E-8
0.0000001
0.000000089
0.0000000888000000000