Kurt Bourbaki
Kurt Bourbaki

Reputation: 12596

Format BigDecimal in Spring

I'm using Spring 3 with JPA.

I've got my Product class like this:

@Entity
@Table(name="products")
public class Product {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @Column(name="price", precision=12, scale=2)    
    private BigDecimal price;

The problem is that when I fill my Product creation form with a price like "1.20" I get in my views "1.2". The trailing zero disappears.

How can I solve this problem?

Upvotes: 3

Views: 4424

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

You can use the JSTL format taglib to format the display of a BigDecimal.

To format a BigDecimal include a Product as an attribute and reference the price field via the instance of Product using JSP EL.

<fmt:formatNumber value="${product.price }" minFractionDigits="2"/>

You should also include the JSTL FMT taglib:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

And will need to place the JSTL API & Implementation Jars on your classpath:

Jar Files

Upvotes: 5

Related Questions