欧洲版Android版的NumberFormatException? [英] NumberFormatException on European Versions of Android?
问题描述
我有一个应用程序启动时运行以下两行代码:
I have an app which runs the following two lines of code upon starting:
DecimalFormat decim = new DecimalFormat("#.00");
return Double.parseDouble(decim.format(totalNumberOfCredits));
当我在美式手机上启动应用程序时,价值 decim。格式(totalNumberOfCredits)
是 .00
。
When I start the app on my American phone, the value of decim.format(totalNumberOfCredits)
is .00
.
然而,在我的Google Play开发者控制台中,我有十几次崩溃,其中的所有内容如下所示:
However, in my Google Play Developer Console, I have a dozen crashes, all of which look like this:
Caused by: java.lang.NumberFormatException: Invalid double: ",00"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:269)
at java.lang.Double.parseDouble(Double.java:295)
DecimalFormat是否真的可以在欧洲手机上生成小数的逗号版本?
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
推荐答案
DecimalFormat真的可以在欧洲手机上生成小数的逗号版本吗?
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
$ b $是的,绝对的。这就是要完成,毕竟:
使用给定的模式和默认语言环境的符号创建一个DecimalFormat。当国际化不是主要关注时,这是获取DecimalFormat的一种方便的方法。
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
要获取给定语言环境的标准格式,请使用NumberFormat上的工厂方法,例如getNumberInstance 。这些工厂将为给定的区域设置返回最合适的NumberFormat子类。
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.
请注意,这不是一个欧洲版Android - 这只是在默认语言环境使用,
作为小数分隔符的上下文中使用Android的问题。
Note that this isn't a matter of a "European version of Android" - it's just a matter of using Android in a context where the default locale uses ,
as the decimal separator.
如果要使用特定区域设置的符号,但使用特定模式,可以使用:
If you want to use the symbols for a particular locale, but using a specific pattern, you could use:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat format = new DecimalFormat("#.00", symbols);
说完了,你一直都不清楚你在做什么 - 你为什么要格式化然后解析一个数字?当您不需要它们时,几乎总是避免使用字符串转换。为什么不直接转换? (我们不知道什么 totalNumberOfCredits
是没有帮助的。)
Having said that, it's not at all clear what you're trying to do in the first place - why would you format and then parse a number? You should almost always avoid string conversions when you don't really need them. Why not just convert it directly? (We don't know what totalNumberOfCredits
is, which doesn't help.)
这篇关于欧洲版Android版的NumberFormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!