将科学记数法转换为十进制记数法 [英] Convert scientific notation to decimal notation

查看:177
本文介绍了将科学记数法转换为十进制记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO上有一个类似的问题,建议使用我已经完成的NumberFormat。

There is a similar question on SO which suggests using NumberFormat which is what I have done.

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

I am using the parse() method of NumberFormat.

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

我真的没想到这会起作用,因为1.930000000000E + 02是一个非常不寻常的数字,我是否必须首先进行一些字符串解析才能删除零?或者有一种快速而优雅的方式吗?

I didn't really expect this to work because 1.930000000000E+02 is a pretty unusual looking number, do I have to do some string parsing first to remove the zeros? Or is there a quick and elegant way?

推荐答案

当您使用带有科学记数法的表达式的DecimalFormat时,您需要指定一个图案。尝试类似

When you use DecimalFormat with an expression in scientific notation, you need to specify a pattern. Try something like

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

参见用于DecimalFormat的javadocs - 有一个标记为科学记数法的部分。

See the javadocs for DecimalFormat -- there's a section marked "Scientific Notation".

这篇关于将科学记数法转换为十进制记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆