229

I have this little crazy method that converts BigDecimal values into nice and readable Strings.

private String formatBigDecimal(BigDecimal bd){
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(3);
    df.setMaximumFractionDigits(3);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(3);
    df.setGroupingSize(20);
    return df.format(bd);
}

It however, also produces a so called grouping separator "," that makes all my values come out like this:

xxx,xxx

I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this little feat?

I have read this and in particular this to death now but I cannot find a way to get this done. Am I approaching this the wrong way? Is there a much more elegant way of doing this? Maybe even a solution that accounts for different local number representations, since the comma would be perfect by European standards.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
MiraFayless
  • 2,353
  • 3
  • 15
  • 8
  • See also http://stackoverflow.com/questions/5236056/force-decimal-point-as-seperator-in-java – Vadzim May 03 '14 at 15:20
  • Change the language, it worked for me. https://stackoverflow.com/questions/4947484/how-to-set-eclipse-console-locale-language?answertab=active#tab-top – Alper Şekercioğlu Oct 25 '20 at 16:03

8 Answers8

382

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

If you want the grouping separator to be a point, you can use an european locale:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

currentLocale can be obtained from Locale.getDefault() i.e.:

Locale currentLocale = Locale.getDefault();
Francisco M
  • 163
  • 2
  • 9
Chris
  • 7,864
  • 1
  • 27
  • 38
  • 1
    you can use applyPattern on the decimal format to get your .00 – krystan honour Oct 29 '14 at 13:27
  • 1
    I have changed otherSymbols.setDecimalSeparator(','); as otherSymbols.setDecimalSeparator('.'); Thanks .It works for me . – Aakanksha Jul 03 '15 at 15:20
  • 2
    it's how the format looks like, e.g. `String formatString = "#,###,###,##0.00";` see https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html for syntax – Vulpo Mar 31 '16 at 13:55
  • 2
    "You can use an european locale". Well, you cannot use any locale from Europe because `Locale.UK` will return `1,567.90 ` and not 1.567,90 – Junior Mayhé Jun 10 '17 at 21:54
  • Side note: `NumberFormat.getNumberInstance()` can run unusually slowly on some Android 7 devices. An alternative is `String.format()` which runs quickly. See https://stackoverflow.com/questions/2379221/java-currency-number-format – Mr-IDE Oct 16 '17 at 05:12
  • @Chris, Can you please guide me for "###.###,00" this pattern. As by using this it gives me Malformed pattern "###.###,00" exception. – Zenny Aug 27 '21 at 07:25
  • What is currentLocale and what do I need it for? – Sergey Zolotarev Dec 21 '22 at 22:49
  • 1
    I tried your method, but it refused to work until the moment I called the df.setDecimalFormatSymbols(otherSymbols); method (thank you, Oracle documentation). Consider including that information in your answer – Sergey Zolotarev Dec 21 '22 at 23:23
  • For `NumberFormat.getCurrencyInstance` you need to use `setMonetaryGroupingSeparator` and `setMonetaryDecimalSeparator` – Arturo Volpe Feb 03 '23 at 13:49
16

Europe is quite huge. I'm not sure if they use the same format all over. However this or this answer will be of help.

String text = "1,234567";
NumberFormat nf_in = NumberFormat.getNumberInstance(Locale.GERMANY);
double val = nf_in.parse(text).doubleValue();

NumberFormat nf_out = NumberFormat.getNumberInstance(Locale.UK);
nf_out.setMaximumFractionDigits(3);
String output = nf_out.format(val);

I.e. use the correct locale.

Reno
  • 33,594
  • 11
  • 89
  • 102
  • 2
    Indeed sir! In the UK "£1,234.00" is to be seen, while in France "1 234,00€" is expected (group separator, decimal separator, currency symbol and currency position have changed). – Vulpo Mar 31 '16 at 14:02
11

This worked in my case:

DecimalFormat df2 = new DecimalFormat("#.##");           
df2.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
11
public String getGermanCurrencyFormat(double value) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    nf.setGroupingUsed(true);
    return "€ " + nf.format(value);
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Imran Khan
  • 119
  • 1
  • 2
5

BigDecimal does not seem to respect Locale settings.

Locale.getDefault(); //returns sl_SI

Slovenian locale should have a decimal comma. Guess I had strange misconceptions regarding numbers.

a = new BigDecimal("1,2") //throws exception
a = new BigDecimal("1.2") //is ok

a.toPlainString() // returns "1.2" always

I have edited a part of my message that made no sense since it proved to be due the human error (forgot to commit data and was looking at the wrong thing).

Same as BigDecimal can be said for any Java .toString() functions. I guess that is good in some ways. Serialization for example or debugging. There is an unique string representation.

Also as others mentioned using formatters works OK. Just use formatters, same for the JSF frontend, formatters do the job properly and are aware of the locale.

Neikius
  • 173
  • 2
  • 9
3
String money = output.replace(',', '.');
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Thakhani Tharage
  • 1,288
  • 16
  • 19
  • 6
    Number can be in arabic format, and they uses another separator: (٫) (in hex U+066B). https://en.wikipedia.org/wiki/Decimal_mark#Other_numeral_systems Numbers are diferent too: https://en.wikipedia.org/wiki/Arabic_(Unicode_block) – mtrakal Aug 17 '16 at 10:03
2

you could just use replace function before you return the string in the method

return df.format(bd).replace(",", ".")
1

This worked for me...

    double num = 10025000;
    new DecimalFormat("#,###.##");
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
    System.out.println(df.format(num));