1

I have a BigDecimal whose value I'd like to convert to a string and NOT lose any precision.

The format I'd like to use is ###.###,## (comma for thousands, period for decimals).

The only way I've made this work is using

DecimalFormat formatter = new DecimalFormat("###.###,##");
formatter.format(bd.doubleValue());

... but I'm afraid I might lose information this way, and precision is a must because I'm dealing with currency (every penny counts).

Additional information: I will only be dealing with sums of up to 1 million if that is of any help.

blashyrk
  • 107
  • 1
  • 6

1 Answers1

1

To be on the safe side, you could multiple your BigDecimal by 100.
Then get its intValue -> say N. Then get N/100 and N%100.
This way you cannot lose precision (N <= 100 million cannot overflow int).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159