How to set thousands separator in Java?
JavaNumber FormattingBigdecimalJava Problem Overview
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.
Java Solutions
Solution 1 - Java
You can use format function with ",";
int no = 124750;
String str = String.format("%,d", no);
//str = 124,750
"," includes locale-specific grouping characters.
Solution 2 - Java
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.
Solution 3 - Java
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()));
Solution 4 - Java
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"
Solution 5 - Java
If you are using thousand separator for Integer data type use 1.
- For integer data Type
String.format("%,d\n", 58625) and output will be 58,625
- For Floating Point data Type String.format("%,.2f",58625.21) and output will be 58,625.21
Solution 6 - Java
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);
Solution 7 - Java
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
Solution 8 - Java
public String formatStr(float val) {
return String.format(Locale.CANADA, "%,.2f", val);
}
formatStr(2524.2) // 2,254.20
Solution 9 - Java
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);
Solution 10 - Java
NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);
Solution 11 - Java
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));
Solution 12 - Java
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