将 long 传递给 Double.parseDouble
当我将 100L 这样的数字传递给 Double.parseDouble() 时 我收到 NumberFormatException。 但 100L 是一个有效的数字。
编辑:如果通过 100d 或 100f,我没有收到任何错误。我只买100L
When I pass in a number like 100L to Double.parseDouble()
I get a NumberFormatException.
But 100L is a valid number.
EDIT: I do not get any error is if pass 100d or 100f. I only get it for 100L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
100L 是一个代表长值 100 的文字。字符串“100L”不是,所以当你将它传递给 parseDouble() 时,它会正确地抱怨。去掉“L”,你应该没问题。
更新:并不是
parseDouble()
不喜欢文字语法,而是它不喜欢您将数字显式声明为long
(实际上它正在寻找浮点类型。)100L is a literal that represents the long value 100. The string "100L" is not, so when you pass it to parseDouble() it rightfully complains. Drop the "L" and you should be fine.
Update: It's not that
parseDouble()
doesn't like the literal syntax, it's that it doesn't like that you've explicitly declared the number as being along
(when in fact it's looking for a floating point type.)从 JavaDocs 中, Double.parseDouble();
语法,
它期望将字符串作为参数传递。 “100L”是一个具有 long 值的文字,而不是字符串。为避免任何错误,请尝试使用“100”而不是“100L”。
parseDouble () 调用 valueOf() ,专门寻找
浮点表示。由于 d 和 f 表示 double 和 float ,所以一切正常。
但是,您不应该明确指定那么长的数字。 (如100L)
仅供参考,
NumberFormatException:http://www.ideone.com/DMpUN
正常运行:http://www.ideone.com/QdxXY
From the JavaDocs, Double.parseDouble();
Syntax,
It expects a string to be passed as argument. "100L" is a literal having value long, not a string. To avoid any error, try using "100" instead of "100L".
parseDouble() calls valueOf(), which specifically looks for
floating point representations.Since, d and f denote doubles and floats , everything works fine.
But, you shouldn't specifiy the number explicitly as long. ( like 100L)
FYI,
NumberFormatException : http://www.ideone.com/DMpUN
Runs normally : http://www.ideone.com/QdxXY
“字符串”中的 100l 不是有效数字。您只需传入一个有效的双字符串即可。
这很好用。正如另一位用户指出的那样,在字符串中传递“100l”是行不通的,因为 l 意味着长值作为文字数字。
100l in a "String" is not a valid number. You simply need to pass in a valid double string.
This works fine. As another user noted passing "100l" in the string wont work because the l implies the long value as a literal number.