Java:将科学记数法转换为常规int

时间:2010-03-30 14:50:20

标签: java

如何将科学记数法转换为常规int 例如:1.23E2 我想将其转换为123

感谢。

6 个答案:

答案 0 :(得分:32)

如果您将值作为字符串,则可以使用

int val = new BigDecimal(stringValue).intValue();

答案 1 :(得分:2)

您可以将其转换为int as:

double d = 1.23E2; // or float d = 1.23E2f;
int i = (int)d; // i is now 123

答案 2 :(得分:2)

我假设你把它作为一个字符串。

看一下DecimalFormat课程。大多数人使用它来将数字格式化为字符串,但它实际上有一个解析方法反过来!用模式初始化它(参见教程),然后在输入字符串上调用parse()。

答案 3 :(得分:2)

查看DecimalFormat.parse()

示例代码:

DecimalFormat df = new DecimalFormat();
Number num = df.parse("1.23E2", new ParsePosition(0));
int ans = num.intValue();
System.out.println(ans); // This prints 123

答案 4 :(得分:0)

你也可以使用这样的东西。

(int) Double.parseDouble("1.23E2")

答案 5 :(得分:-1)

您可以实施自己的解决方案:

String string = notation.replace(".", "").split("E")[0]
相关问题