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.
This question is related to
java
number-formatting
bigdecimal
You can use format function with ",";
int no = 124750;
String str = String.format("%,d", no);
//str = 124,750
"," includes locale-specific grouping characters.
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);
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
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);
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"
NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);
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));
If you are using thousand separator for Integer data type use 1.
String.format("%,d\n", 58625) and output will be 58,625
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()));
Source: Stackoverflow.com