简体   繁体   English

使用错误的小数分隔符在Android格式化货币

[英]Formatting currency in Android using wrong decimal separator

I got a bug report from a Swedish user saying that our Swedish currency was using the wrong decimal separator. 我收到瑞典用户的错误报告,说我们的瑞典货币使用了错误的小数分隔符。

NumberFormat enUS = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat enGB = NumberFormat.getCurrencyInstance(Locale.UK);
NumberFormat svSE = NumberFormat.getCurrencyInstance(new Locale("sv", "SE"));
double cost = 1020d;
String fmt = "en_US: %s en_GB %s sv_SE %s";
String text = String.format(fmt, enUS.format(cost), enGB.format(cost), svSE.format(cost));
Log.e("Format", text);

> Format﹕ en_US: $1,020.00 en_GB £1,020.00 sv_SE 1 020:00 kr

They say that the format should be "1 020,00 kr". 他们说格式应为“1 020,00 kr”。 When I inspect the format object, it looks like it has decimalSeparator of "," in the symbols table, but a "monetarySeparator" of ":". 当我检查格式对象时,它看起来在符号表中有“,”的decimalSeparator,但是“:”的“currencySeparator”。

Does anyone know if : is actually correct, whether this is a bug in Android/java, or any sort of workaround? 有谁知道:实际上是否正确,这是Android / java中的错误还是任何形式的解决方法?

It's like your user says: In Swedish thousand separator is white space " " and decimal separator is comma "," and currency symbol "kr" (Krona). 这就像你的用户说:瑞典千分隔符是空格“”,小数分隔符是逗号“,”和货币符号“kr”(克朗)。 So colon ":" is definitely wrong. 所以冒号“:”绝对是错误的。

You can check it here too: http://www.localeplanet.com/java/sv-SE/ 你也可以在这里查看: http//www.localeplanet.com/java/sv-SE/

What Java version are you using? 您使用的Java版本是什么? It works well on my desktop 1.6.0_13 它在我的桌面1.6.0_13上运行良好

-- update -- - 更新 -

It seems that on Android there's a bug, but you can go around the bug by using the DecimalFormatSymbols like this: 似乎在Android上有一个bug,但你可以通过使用像这样的DecimalFormatSymbols解决这个问题:

    DecimalFormat svSE = new DecimalFormat("#,###.00");
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("sv", "SE"));
    symbols.setDecimalSeparator(',');
    symbols.setGroupingSeparator(' ');
    svSE.setDecimalFormatSymbols(symbols);

This prints the correct separators in Android as well. 这也会在Android中打印正确的分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM