[Java] Formatting a number with right dots and commas

Formatting a number in the proper way is very important for the final user. And if you are in Locale different from the US one, then you can encounter some users that complain against it.

For example I’m Italian and we use as decimal separator a comma (,) and as thousands separator a dot (.). The opposite of the US. However the DecimalFormat accept only dots and commas in the US way. You can use the DecimalFormatSymbols in order to “translate” the format.

DecimalFormat df = new DecimalFormat("#,##0.00");
df.setDecimalFormatSymbos(new DecimalFormatSymbols(Locale.ITALY));
System.out.println(df.format(new BigDecimal(123456,78))); //will output 123.456,78
Advertisements

2 thoughts on “[Java] Formatting a number with right dots and commas

  1. Ashok Patel says:

    This works great on desktop computer. I could not make it work for Android (Mobile) application.
    Maybe it’s just my device (I hope).

  2. Kevin says:

    Right, doesn’t work on android devices. Works in Firefox on android but unfortunately not in Chrome, Opera and Dolphin…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s