NumberFormatException的Android上的欧洲版本? [英] NumberFormatException on European Versions of Android?
问题描述
我有一个在启动运行code以下两行的应用程序:
DecimalFormat的decim =新的DecimalFormat(#00);
返回Double.parseDouble(decim.format(totalNumberOfCredits));
当我启动应用程序,我的美国手机上,的decim.format值(totalNumberOfCredits)
是 .00
。
不过,在我的谷歌Play开发者控制台,我有一打崩溃,所有这一切看起来是这样的:
产生的原因:java.lang.NumberFormatException:无效的双,00
在java.lang.StringToReal.invalidReal(StringToReal.java:63)
在java.lang.StringToReal.parseDouble(StringToReal.java:269)
在java.lang.Double.parseDouble(Double.java:295)
难道真的可能是DecimalFormat的欧洲手机生产小数点逗号的版本?
难道真的可能是DecimalFormat的欧洲手机生产小数点逗号的版本?
块引用>是的,绝对。这就是它的<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#DecimalFormat%28java.lang.String%29\">meant做,毕竟:
使用给定的模式和默认语言环境的符号创建一个DecimalFormat。这是获得一个DecimalFormat当国际化不是主要问题的便捷方式。
要获得标准格式为给定的语言环境,使用NumberFormat的工厂方法,如getNumberInstance。这些工厂将返回最适合的NumberFormat子类的指定区域设置。
块引用>请注意,这不是一个欧洲版本的Android的问题 - 它只是一个在上下文中,其中默认的语言环境使用
,
为使用Android的事小数点分隔符。如果您要使用的符号特定的语言环境,但使用一个特定的模式,您可以使用:
的DecimalFormatSymbols符号= DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat的格式=新DecimalFormat的(,符号#00);话虽如此,它不是完全清楚你想要什么摆在首位的事 - 你为什么要格式化,然后分析了一些?你应该总是避免字符串转换时,你并不真正需要它们。为什么不把它转换直接? (我们不知道是什么
totalNumberOfCredits
是,它并不能帮助。)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));
When I start the app on my American phone, the value of
decim.format(totalNumberOfCredits)
is.00
.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)
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
解决方案Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
Yes, absolutely. That's what it's meant to do, after all:
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.
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.
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);
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.)这篇关于NumberFormatException的Android上的欧洲版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!