为什么 String.format("%.2f", 10.0F) returns 值 10,00 而不是 10.00?
Why String.format("%.2f", 10.0F) returns value 10,00 instead of 10.00?
更新:删除上述问题中的 "in Android 5.0",因为我觉得它与版本无关,但取决于语言环境。
下面代码的通常输出是10.00。但在 Android 5.0 中它 returns 值 10,00.
String value = String.format("%.2f", 10.0F);
System.out.println("Value is "+value); //For Console Output
Log.w("NumberClass.java","Value is "+value); //For Logcat Output
是
的功能
String.format("%.2f", 10.0F);
android 棒棒糖有变化吗?如果那么,这种奇怪行为的解决方案是什么?
我需要 10.00 作为输出。
String.format()
的文档说:
Returns a localized formatted string, using the supplied format and
arguments, using the user's default locale.
因此,如果您是法国人,则会使用逗号,但如果您是美国人,则会使用点。
您可能想使用 String.format (Locale locale, String format, Object... args)
(这是您当前使用的函数的重载)来获得所需的行为。 (参见文档 here). You can give it an initialized Locale
to get the behaviour that you want. For more information on Locale
check the documentation here.
试试这个
String value2 = String.format(getResources().getConfiguration().locale,"%.2f", 10.0F);
更新:删除上述问题中的 "in Android 5.0",因为我觉得它与版本无关,但取决于语言环境。
下面代码的通常输出是10.00。但在 Android 5.0 中它 returns 值 10,00.
String value = String.format("%.2f", 10.0F);
System.out.println("Value is "+value); //For Console Output
Log.w("NumberClass.java","Value is "+value); //For Logcat Output
是
的功能 String.format("%.2f", 10.0F);
android 棒棒糖有变化吗?如果那么,这种奇怪行为的解决方案是什么?
我需要 10.00 作为输出。
String.format()
的文档说:
Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.
因此,如果您是法国人,则会使用逗号,但如果您是美国人,则会使用点。
您可能想使用 String.format (Locale locale, String format, Object... args)
(这是您当前使用的函数的重载)来获得所需的行为。 (参见文档 here). You can give it an initialized Locale
to get the behaviour that you want. For more information on Locale
check the documentation here.
试试这个
String value2 = String.format(getResources().getConfiguration().locale,"%.2f", 10.0F);