Android NumberFormatException: Invalid Double - except the value is a valid Double

非 Y 不嫁゛ 提交于 2019-12-01 06:41:02

问题


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"

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)

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?


回答1:


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

EDIT

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



回答2:


For double constants, the language asks you to use the form -0.05d (with the "d" suffix). See this for reference

Also make sure that the symbol intended to be a minus sign is a "hyphen" and not a "long em dash".



来源:https://stackoverflow.com/questions/22201159/android-numberformatexception-invalid-double-except-the-value-is-a-valid-doub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!