java.lang.NumberFormatException用于将字符串转换为long [英] java.lang.NumberFormatException for converting string to long
问题描述
我正在尝试将字符串转换为long并抛出 NumberFormatException
.我认为这根本不超出 long
的范围.
I am trying to convert a string to long and it throws the NumberFormatException
. I don't think it is beyond range of long
at all.
这是要转换的代码,其中 count_strng
是我要转换为long的字符串. trim()
函数没有任何区别.
Here is the code to convert, where count_strng
is the String I want to convert to long. trim()
function is not making any difference.
long sum_link = Long.parseLong(count_strng.trim());
这是堆栈跟踪.
java.lang.NumberFormatException: For input string: "0.003846153846153846"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.parseLong(Long.java:483)
有人知道这里到底是什么问题吗?
Anyone knows what is the exact issue here?
推荐答案
Long.parseLong()
试图将输入字符串解析为 long
.在Java中, =长定义为/a>:
Long.parseLong()
is trying to parse the input string into a long
. In Java, a long
is defined such that:
long数据类型是64位二进制补码整数.
The long data type is a 64-bit two's complement integer.
一个整数(来自拉丁文整数,意思是"whole")是可以写而没有小数部分的数字.
An integer (from the Latin integer meaning "whole") is a number that can be written without a fractional component.
您收到的错误表明您要解析的输入字符串是"0.003846153846153846"
,显然,确实有个小数部分.
The error you are getting shows the input string you are trying to parse is "0.003846153846153846"
, which clearly does have a fractional component.
如果要解析浮点数,则应使用 Double.parseDouble()
.
You should use Double.parseDouble()
if you want to parse a floating point number.
这篇关于java.lang.NumberFormatException用于将字符串转换为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!