Separate numbers with spaces in groups of 3 in java

Is there a way to split a double number in java so that all groups of 3 digits are separated by a space and only 2 digits after the decimal point? It is easy to separate them with a comma:

DecimalFormat df = new DecimalFormat("###,###.00"); df.format(number); 

So 235235.234 turns into 234,234.23

I need 234 234.23

How can i do this?

+8
java split numbers
source share
1 answer

I believe that the comma in your format string is not really a comma - it is just the grouping symbol in DecimalFormatSymbols that you use.

Try the following:

 // TODO: Consider specifying a locale DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(' '); DecimalFormat df = new DecimalFormat("###,###.00", symbols); 

Or as an alternative to the last line:

 DecimalFormat df = new DecimalFormat(); df.setDecimalFormatSymbols(symbols); df.setGroupingSize(3); df.setMaximumFractionDigits(2); 
+17
source share

All Articles