Search code examples
javabigdecimal

Convert Bigdecimal 123 to 123.00


Below is the piece of code, I am trying to print 123 as 123.00 but it gives me 123:

BigDecimal decimal = new BigDecimal(123);
decimal.doubleValue(); 

Can you please help me in printing 123 as 123.00?


Solution

  • Try like this:

    BigDecimal value = new BigDecimal(123);
    value = value.setScale(2, BigDecimal.ROUND_HALF_EVEN)
    

    You can refer Javadocs for ROUND_HALF_EVEN and setScale