0

I'd like to format and display BigDecimal numbers with always to fractions. No other separators should ever be displayed. How could I achieve it? The following does not work.

NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMANY);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumIntegerDigits(2);
Sysout(formatter.format(new BigDecimal(100))); //would expect "100,00", but printout is "100"
membersound
  • 81,582
  • 193
  • 585
  • 1,120

4 Answers4

5

The setMinimumFractionDigits will set the minimum number of digits in the fraction portion of the number.

NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMANY);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumIntegerDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println((formatter.format(new BigDecimal(100)))); // prints "100,00"
M A
  • 71,713
  • 13
  • 134
  • 174
2

Just use a custom format of

0.00

Like so:

NumberFormat formatter = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.GERMANY));

This will force any incoming number for have to decimals.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

you can use this.

 aBigDecimal.SetScale(2, RoundingMode.HALF_UP);
Arno_Geismar
  • 2,296
  • 1
  • 15
  • 29
0
BigDecimal d=new BigDecimal("100");
d=d.setScale(2, RoundingMode.CEILING);
System.out.println(d);
Abhishek Mishra
  • 611
  • 4
  • 11