176

How to set thousands separator in Java?
I have String representation of a BigDecimal that I want to format with a thousands separator and return as String.

0

12 Answers 12

298

You can use format function with ",";

int no = 124750;
String str = String.format("%,d", no);

//str = 124,750

"," includes locale-specific grouping characters.

docs

9
  • 16
    What If I want to use a . as a separator ?
    – Enissay
    Jan 12, 2015 at 18:22
  • 1
    As I usually use String.format it's the best and easiest for me!
    – Ali
    Apr 21, 2015 at 12:22
  • 27
    @Enissay Call format() with an explicit locale (Local.US is known to use , as a separator) and then replace , with your custom separator: String.format(Locale.US, "%,d", n).replace(',', '.').
    – minipif
    May 26, 2015 at 0:30
  • java.util.IllegalFormatConversionException: %d can't format java.lang.String arguments Aug 19, 2016 at 11:22
  • 3
    @ImanMarashi Cast it to an int. Or a long. Or a BigInteger. And use f if you want to use double, float or BigDecimal.
    – Adowrath
    Sep 5, 2016 at 17:50
135

This should work (untested, based on JavaDoc):

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

According to the JavaDoc, the cast in the first line should be save for most locales.

5
  • 16
    Untested comment on this: Javadoc for getDecimalFormatSymbols() says: Returns: a copy of the desired DecimalFormatSymbols. So you should use setDecimalFormatSymbols(theCopy) after altering the copy. Sep 5, 2011 at 22:43
  • 6
    The docs for bd.longValue() say "any fractional part will be discarded". So I don't know if this is the best way to do this if you care about precision
    – codinguser
    Jul 17, 2012 at 19:29
  • 2
    I guess you could use new and set it back to formatter: DecimalFormatSymbols customSymbol = new DecimalFormatSymbols(); customSymbol.setDecimalSeparator(decimalSeperator.charAt(0)); customSymbol.setGroupingSeparator(thousandSeperator); formatter.setDecimalFormatSymbols(customSymbol); Aug 5, 2014 at 9:50
  • 2
    I think you would want to use \u00a0 (non-breaking space) as grouping separator. It is the default for some locales.
    – ACV
    Oct 25, 2016 at 7:41
  • 5
    Why do you call longValue? The question was to set thousands operator for BigDecimal, not long. Feb 26, 2017 at 19:23
64
BigDecimal bd = new BigDecimal(300000);

NumberFormat formatter = NumberFormat.getInstance(new Locale("en_US"));

System.out.println(formatter.format(bd.longValue()));

EDIT

To get custom grouping separator such as space, do this:

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setGroupingSeparator(' ');

DecimalFormat formatter = new DecimalFormat("###,###.##", symbols);
System.out.println(formatter.format(bd.longValue()));
4
  • i know i need to use DecimalFormatSymbols setGroupingSeparator, but i don't know how to apply it on my BigDecimal
    – Funtime
    Mar 16, 2011 at 10:07
  • 1
    @Funtime: you don't "apply it on your BigDecimal". You build a NumberFormat object with the desired properties and use that to format your BigDecimal. A BigDecimal only has a specified value, it does not have a specified format. Mar 16, 2011 at 10:10
  • 1
    Note that the thousands separator is generally a non breaking space (when it is a space)
    – cquezel
    Jul 23, 2012 at 19:59
  • Had to change the top line in the 2nd option to DecimalFormatSymbols symbols = new DecimalFormatSymbols(); for this to work.
    – PaperThick
    Mar 14, 2013 at 14:05
25

If you are using thousand separator for Integer data type use 1.

1. For integer data Type

String.format("%,d\n", 58625)

Output:

58,625

2. For Floating Point data Type

String.format("%,.2f",58625.21)

Output:

58,625.21
1
22

try this code to format as used in Brazil:

    DecimalFormat df = new DecimalFormat(
      "#,##0.00", 
      new DecimalFormatSymbols(new Locale("pt", "BR")));

    BigDecimal value = new BigDecimal(123456.00);

    System.out.println(df.format(value.floatValue()));

    // results: "123.456,00"
1
  • 2
    You too try this, in case of JasperReports, forcing the format correct: parameters.put("REPORT_LOCALE", new Locale("pt", "BR")); Send this parameter to report.
    – deldev
    Jan 27, 2015 at 18:40
7
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setDecimalSeparator('|');
formatSymbols.setGroupingSeparator(' ');

String strange = "#,##0.###";
DecimalFormat df = new DecimalFormat(strange, formatSymbols);
df.setGroupingSize(4);

String out = df.format(new BigDecimal(300000).doubleValue());

System.out.println(out);
6

As mentioned above, the following link gives you the specific country code to allow Java to localize the number. Every country has its own style.

In the link above you will find the country code which should be placed in here:

...(new Locale(<COUNTRY CODE HERE>));

Switzerland for example formats the numbers as follows:

1000.00 --> 1'000.00

country code

To achieve this, following codes works for me:

NumberFormat nf = NumberFormat.getNumberInstance(new Locale("de","CH"));
nf.setMaximumFractionDigits(2);
DecimalFormat df = (DecimalFormat)nf;
System.out.println(df.format(1000.00));

Result is as expected:

1'000.00
5
public String formatStr(float val) {
 return String.format(Locale.CANADA, "%,.2f", val);
}

formatStr(2524.2) // 2,254.20



1
  • 2
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
    – Borja
    Mar 17, 2020 at 7:47
4

The accepted answer has to be really altered otherwise not working. The getDecimalFormatSymbols makes a defensive copy. Thus,

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

The new line is this one: formatter.setDecimalFormatSymbols(symbols);

4
NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);
0
4

For decimals:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
DecimalFormat dfDecimal = new DecimalFormat("###########0.00###");
dfDecimal.setDecimalFormatSymbols(symbols);
dfDecimal.setGroupingSize(3);
dfDecimal.setGroupingUsed(true);
System.out.println(dfDecimal.format(number));
2

Use underscore (_) to make literal numeric values in the code itself more readable:

int example = 12_004_953; // Twelve million four thousand nine hundred fifty-three

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.