However, if you really want to use BigDecimal you can do
String dividendAmountStr = String.format("%.2f", dividendAmount);
Edited by: Peter Lawrey on Jul 11, 2011 3:55 PM
String dividendAmountStr = BigDecimal.valueOf(dividendAmount).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
double d = 0.1; System.out.println("d= " + Double.toString(d)); BigDecimal bd = new BigDecimal(d); System.out.println("bd= " + bd); BigDecimal bd2 = BigDecimal.valueOf(d); System.out.println("bd2= " + bd2);
As you can see the second method for converting a double doesn't have this round issue for this value. This might be a temping option and it might work most of the time, however using setScale() with a rounding option will work every time.
d= 0.1 bd= 0.1000000000000000055511151231257827021181583404541015625 bd2= 0.1