且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android NumberFormatException:无效的Double - 除了值是有效的Double

更新时间:2023-11-29 16:23:04

它是或不是有效的双,取决于你的区域设置。使用美国/英语语言环境, -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