java指数表示法,Java以指数表示法解析数字

当尝试将包含浮点或科学计数法的字符串(如'9.18E+09')转换为长整型时,会抛出NumberFormatException。一个可行的解决方案是使用Double.valueOf()方法,然后将其转换为long。例如,Double.valueOf("9.18E+09").longValue()可以避免异常并得到期望的数值。
摘要由CSDN通过智能技术生成

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.

解决方案

Use Double.valueOf() and cast the result to long:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值