Reputation: 2624
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.
Upvotes: 188
Views: 246452
Reputation: 601
public String formatStr(float val) {
return String.format(Locale.CANADA, "%,.2f", val);
}
formatStr(2524.2) // 2,254.20
Upvotes: 5
Reputation: 5139
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
Upvotes: 32
Reputation: 24582
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
Upvotes: 1
Reputation: 71
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
Upvotes: 7
Reputation: 4003
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));
Upvotes: 4
Reputation: 114797
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.
Upvotes: 139
Reputation: 8333
NumberFormat nf = DecimalFormat.getInstance(myLocale);
DecimalFormatSymbols customSymbol = new DecimalFormatSymbols();
customSymbol.setDecimalSeparator(',');
customSymbol.setGroupingSeparator(' ');
((DecimalFormat)nf).setDecimalFormatSymbols(customSymbol);
nf.setGroupingUsed(true);
Upvotes: 4
Reputation: 21585
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);
Upvotes: 4
Reputation: 1386
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"
Upvotes: 25
Reputation: 4709
You can use format function with ",";
int no = 124750;
String str = String.format("%,d", no);
//str = 124,750
"," includes locale-specific grouping characters.
Upvotes: 307
Reputation: 62603
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()));
Upvotes: 65
Reputation: 12585
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);
Upvotes: 7