Search code examples
javabigdecimal

Formatting big decimal


I have a big decimal with value:                              -6
I need to format it in order to obtain:       -000000006.00
If my big decimal is 6, the result will be: 0000000006.00

I am trying to use String.format, but I can't get that result.

String.format("%010d", invoice.getAmount().doubleValue());

How can I format my big decimal to get the desired results?


Solution

  • String result = String.format("%013.2f", invoice.getAmount());
    

    Note that the symbol used for the decimal point is locale-specific; you might for example get , instead of .. If you need it to always be dot . then specify a locale for which the decimal point is a dot, for example:

    String result = String.format(Locale.US, "%013.2f", invoice.getAmount());