Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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". When I inspect the format object, it looks like it has decimalSeparator of "," in the symbols table, but a "monetarySeparator" of ":".

Does anyone know if : is actually correct, whether this is a bug in Android/java, or any sort of workaround?

like image 820
Paul Avatar asked Jan 15 '14 17:01

Paul


People also ask

What is Decimal format?

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.

What is the currency format in Java?

Java provides an automated way for formatting currencies depending on the locale. getCurrencyInstance() is a static method of the NumberFormat class that returns the currency format for the specified locale. Note: A Locale in Java represents a specific geographical, political, or cultural region.


1 Answers

It's like your user says: In Swedish thousand separator is white space " " and decimal separator is comma "," and currency symbol "kr" (Krona). So colon ":" is definitely wrong.

You can check it here too: http://www.localeplanet.com/java/sv-SE/

What Java version are you using? It works well on my desktop 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:

    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.

like image 98
user2832636 Avatar answered Oct 26 '22 11:10

user2832636