CarlosJavier
CarlosJavier

Reputation: 1045

Comma as decimal separator in Java

i want to apply the pattern ###,00 to my Big Decimal numbers, so the ',' be the decimal separator and no grouping separator will be used.

I want to get the string representation of a number with the describe pattern, so i do the following:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
df.setGroupingUsed(false);

DecimalFormat df = new DecimalFormat("###,00", symbols);

But when i perform the next operation:

System.out.println(df.format(new BigDecimal(589555.23)));

The output is 589555 and i am expecting 589555,23 what is wrong? How can i make this works?

Upvotes: 3

Views: 9374

Answers (3)

Ketan
Ketan

Reputation: 406

Instead of using "," in the DecimalFormat use a "."

DecimalFormat df = new DecimalFormat("########.##", symbols);

the output i got was 589555,23

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');


DecimalFormat df = new DecimalFormat("########.##", symbols);
df.setGroupingUsed(false);
System.out.println(df.format(new BigDecimal(589555.23)));

Upvotes: 1

rajuGT
rajuGT

Reputation: 6404

Just change the decimalFormat pattern to ###.00 Because in your format pattern, there is no information related to decimal values(after decimal point), hence decimal values are not displayed in the formatted output.

symbols.setDecimalSeparator(',');
DecimalFormat df = new DecimalFormat("###.00", symbols);

Outputs:

System.out.println(df.format(new BigDecimal(589555.23))); //589555,23

Upvotes: 1

Emanuel Seidinger
Emanuel Seidinger

Reputation: 1272

You can try using a locale where a comma is used as decimal separator. For example:

NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.GERMAN);
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setGroupingUsed(false);
System.out.println(numberFormat.format(new BigDecimal(589555.23)));

Upvotes: 0

Related Questions