Java以指数表示法解析数字 [英] Java parse a number in exponential notation

查看:228
本文介绍了Java以指数表示法解析数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将String转换为整数,我得到一个 NumberFormatException 。原因很明显。但我需要一个解决方法。以下是示例代码。

I am trying to do a conversion of a String to integer for which I get a NumberFormatException. The reason is pretty obvious. But I need a workaround here. Following is the sample code.

public class NumberFormatTest {
 public static void main(String[] args) {
  String num = "9.18E+09";
  try{
   long val = Long.valueOf(num);
  }catch(NumberFormatException ne){
   //Try to convert the value to 9180000000 here
  }
 }
}

我需要评论部分中的逻辑,通用的一个会很好。谢谢。

I need the logic that goes in the comment section, a generic one would be nice. Thanks.

推荐答案

使用 Double.valueOf()并投射结果到

Double.valueOf("9.18E+09").longValue()

这篇关于Java以指数表示法解析数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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