java.lang.NumberFormatException 用于将字符串转换为 long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33176416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:18:27  来源:igfitidea点击:

java.lang.NumberFormatException for converting string to long

javastringnumberformatexception

提问by Keval Shah

I am trying to convert a string to long and it throws the NumberFormatException. I don't think it is beyond range of longat all.

我正在尝试将字符串转换为 long 并且它抛出NumberFormatException. 我不认为它超出了范围long

Here is the code to convert, where count_strngis the String I want to convert to long. trim()function is not making any difference.

这是要转换的代码,count_strng我想转换成long的字符串在哪里。trim()功能没有任何区别。

long sum_link = Long.parseLong(count_strng.trim());

Here is the stacktrace.

这是堆栈跟踪。

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?

任何人都知道这里的确切问题是什么?

回答by azurefrog

Long.parseLong()is trying to parse the input string into a long. In Java, a longis defined such that:

Long.parseLong()正在尝试将输入字符串解析为long. 在 Java 中, along定义为

The long data type is a 64-bit two's complement integer.

long 数据类型是 64 位二进制补码整数。

An integer is defined such that:

一个整数被定义为

An integer (from the Latin integer meaning "whole") is a number that can be written without a fractional component.

整数(来自拉丁整数,意思是“整数”)是一个可以不带小数部分的数字。

The error you are getting shows the input string you are trying to parse is "0.003846153846153846", which clearly does havea fractional component.

您收到的错误显示您尝试解析的输入字符串 is "0.003846153846153846",它显然具有小数部分。

You should use Double.parseDouble()if you want to parse a floating point number.

Double.parseDouble()如果要解析浮点数,则应使用。

回答by SacJn

As your input string is actually not a long, parsing into longwould throw NumberFormatException. Rather try this

由于您的输入字符串实际上不是 a long,解析为long会抛出NumberFormatException. 不如试试这个

Double d = Double.parseDouble(count_strng.trim());
Long l = d.longValue();

回答by Monz

Long types represents mathematical integers. (Integer also represents mathematical integers, but with a smaller range than Long)

Long 类型表示数学整数。(Integer 也代表数学整数,但范围比 Long 小)

Long and Integer cannot represent values that have a decimal point or a fractional component. The parser enforces this rule by rejecting the string you gave it.

Long 和 Integer 不能表示具有小数点或小数部分的值。解析器通过拒绝您提供的字符串来强制执行此规则。

If you want to parse a string that may contain a decimal point and use the resulting value as a Long, you would first have to parse it as a Double and then convert it to a Long.

如果要解析可能包含小数点的字符串并将结果值用作 Long,则必须首先将其解析为 Double,然后将其转换为 Long。

Conversion from Double to Long can be done one of two ways: Truncating the fractional part (basically just ignoring it) and rounding the fractional part mathematically. To truncate, use a cast, to round use the Math.round()method.

从 Double 到 Long 的转换可以通过以下两种方式之一完成:截断小数部分(基本上只是忽略它)和数学四舍五入小数部分。要截断,请使用强制转换,要舍入使用该Math.round()方法。

Hers'a an example:

她的一个例子:

String s = "0.51"; // Greater than 0.50 so will round up
double d = Double.parseDouble(s);

System.out.println(d); // The parsed double

System.out.println((int)d); // Double converted to int and truncated (fractional part dropped)

System.out.println(Math.round(d)); // Double converted to int with mathematical rounding

This code will print

此代码将打印

0.51
0
1

Also: trim()is a String function that removes whitespace characters from the string - it does not do any math.

另外:trim()是一个从字符串中删除空白字符的字符串函数 - 它不做任何数学运算。