Android NumberFormatException:无效的Double - 除了值是有效的Double [英] Android NumberFormatException: Invalid Double - except the value is a valid Double
问题描述
所以有一天,Google Play开发者控制台的崩溃部分弹出以下错误:
So the other day, the following error popped up in the Crashes section of the Google Play Developer Console:
java.lang.NumberFormatException: Invalid double: "−0.05"
现在纠正我,如果我错了,但这实际上是一个有效的双倍 - 它被认为是在我的电脑,模拟器和我自己的Android设备(Nexus 5)上有效的双倍
Now correct me if I'm wrong, but that is in fact a valid double - and it is recognised as a valid double on my computer, on the emulator and on my own Android device (Nexus 5)
该设备它崩溃了一个运行Android 4.3的Galaxy Note II - 任何想法,为什么它可能会崩溃?
The device that it crashed on was a Galaxy Note II running Android 4.3 - any ideas as to why it might be crashing please?
推荐答案
它是或不是有效的双,取决于你的区域设置。使用美国/英语语言环境, -0.05
是一个有效的双倍,但以FRENCH语言环境为例,它不是(它应该是 -0, 05
用逗号)。
It is or isn't a valid double depending on your Locale. With a US/ENGLISH locale, -0.05
is a valid double but with a FRENCH locale for example, it is not (it should be -0,05
with a comma).
您可以通过以下方式看到它:
You can see it in action with:
NumberFormat fmt = NumberFormat.getNumberInstance(Locale.US);
double d = fmt.parse("-0.05").doubleValue(); //-0.05
fmt = NumberFormat.getNumberInstance(Locale.FRENCH);
d = fmt.parse("-0.05").doubleValue(); //-0.0
d = fmt.parse("-0,05").doubleValue(); //-0.05
编辑
但是你的问题可能不是这样。减号无效。您正在使用 -
而不是 -
(它们看起来一样,但不一样的字符)。演示:
However your issue is maybe not that. The minus sign is not valid. You are using −
instead of -
(they look the same but are not the same character). Demo:
Double.parseDouble("-0.05"); //ok
Double.parseDouble("−0.05"); //exception
这篇关于Android NumberFormatException:无效的Double - 除了值是有效的Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!