Search code examples
javabigdecimal

How can I specify the number of decimal places of a BigDecimal to print?


I am using BigDecimal as follows:

BigDecimal val = object.getValue();//it returns BigDecimal type value

val contains 12.2300. I want to display 12.23 on my page. How can I do this?


Solution

  • Use DecimalFormat:

    String formatted = new DecimalFormat("#.##").format(val);