Search code examples
androidbigdecimalnumber-formatting

Android: BigDecimal always show number up to 2 decimal places


String TotalAmount= "";

Lets say I have TotalAmount = 10, or 10.1 or 10.11 or 10.11111

BigDecimal Amount = new BigDecimal(amount);

I want Amount in this pattern #.##

10 -> 10.00
10.1 -> 10.10
10.11 -> 10.11
10.11111 -> 10.11

Solution

  • Solved it myself

    BigDecimal TotalAmount= new BigDecimal(myAmount);
    BigDecimal Amount = TotalAmount.divide(new BigDecimal(1),2, RoundingMode.HALF_UP);