将包含十进制的String转换为Long [英] Converting a String that contains decimal to Long
本文介绍了将包含十进制的String转换为Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下示例(链接到ideone )。
long lDurationMillis = 0;
lDurationMillis = Long.parseLong("30000.1");
System.out.print("Play Duration:" + lDurationMillis);
它抛出异常:
Exception in thread "main" java.lang.NumberFormatException: For input string: "30000.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:419)
at java.lang.Long.parseLong(Long.java:468)
at Main.main(Main.java:9)
但为什么它不会让我直接将该数字转换为字符串?我可以将数字转换为整数而不是转换为双倍。但还有其他方法吗?
But why it wont let me convert that number to a string directly ?I can convert number to integer and than convert to double . But is there any other way ?
推荐答案
值 30000.1
是一个无效的长值。你可以先解析double值:
The value 30000.1
is an invalid long value. You could parse the double value first:
lDurationMillis = (long)Double.parseDouble("30000.1");
这篇关于将包含十进制的String转换为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文