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.
12 Answers
You can use format function with ",";
int no = 124750;
String str = String.format("%,d", no);
//str = 124,750
"," includes locale-specific grouping characters.

- 4,569
- 5
- 34
- 49
-
16What If I want to use a `.` as a separator ? – Enissay Jan 12 '15 at 18:22
-
1As I usually use String.format it's the best and easiest for me! – Ali Apr 21 '15 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 '15 at 00:30
-
java.util.IllegalFormatConversionException: %d can't format java.lang.String arguments – Iman Marashi Aug 19 '16 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 05 '16 at 17:50
-
@Adowrath a `String` cannot be cast to an `int`, to a `long`, to a `float`, to a `BigInteger` or to a `BigDecimal`. – Pere Jan 12 '17 at 16:03
-
@Pere I didn't mean cast literally, but just converting it. – Adowrath Jan 12 '17 at 16:11
-
Small word of warning: This blows up when using `Locale.ROOT`. – Petter Hesselberg May 07 '18 at 13:19
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.

- 1
- 1

- 113,398
- 19
- 180
- 268
-
16Untested comment on this: Javadoc for `getDecimalFormatSymbols()` says: `Returns: a copy of the desired DecimalFormatSymbols`. So you should use `setDecimalFormatSymbols(theCopy)` after altering the copy. – java.is.for.desktop Sep 05 '11 at 22:43
-
6The 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 '12 at 19:29
-
2I 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);` – Lee Yi Hong Aug 05 '14 at 09:50
-
2I think you would want to use \u00a0 (non-breaking space) as grouping separator. It is the default for some locales. – ACV Oct 25 '16 at 07:41
-
5Why do you call longValue? The question was to set thousands operator for BigDecimal, not long. – FINDarkside Feb 26 '17 at 19:23
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()));

- 61,315
- 23
- 138
- 167
-
i know i need to use DecimalFormatSymbols setGroupingSeparator, but i don't know how to apply it on my BigDecimal – Funtime Mar 16 '11 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. – Joachim Sauer Mar 16 '11 at 10:10
-
1Note that the thousands separator is generally a non breaking space (when it is a space) – cquezel Jul 23 '12 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 '13 at 14:05
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

- 552
- 6
- 20

- 4,801
- 3
- 36
- 45
-
1This was an epiphany. However, you really have to stress, that "," is only a flag, not the character it uses as a group separator. (Took me some time to find the issue.) See https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html – Vankog Nov 20 '19 at 11:08
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,296
- 18
- 27
-
2You 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 '15 at 18:40
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);

- 12,427
- 23
- 80
- 112
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
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
public String formatStr(float val) {
return String.format(Locale.CANADA, "%,.2f", val);
}
formatStr(2524.2) // 2,254.20

- 583
- 6
- 8
-
2While 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. – borchvm Mar 17 '20 at 07:47
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);

- 20,677
- 15
- 82
- 117
NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);

- 7,869
- 4
- 37
- 68
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));

- 3,797
- 3
- 41
- 39
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

- 18,032
- 13
- 118
- 133