Search code examples
javajstlbigdecimal

Java BigDecimal formatting


I have a class with BigDecimal field. I put this object to JSP (Spring MVC). And I need to display BigDecimal field without zero in decimal (20.00 like 20), that's why my getter method looks like

public BigDecimal getValue() {
    return value.stripTrailingZeros();
}

And I have the next result:

20.50 = 20.5;
13.25 = 13.25;
30.00 = 3E+1.

How can I change 3E+1 to 30 ? Can I format it with Java or JSTL?


Solution

  • I found a simple solution. It doesn't need to do stripTrailingZeros() in the getter method. Just

    public BigDecimal getValue() {
        return value;
    }
    

    And on the JSP

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
    <fmt:formatNumber value="${object.value}" minFractionDigits="0"/>