Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 392
Kurt Bourbaki Avatar asked Jun 15 '26 05:06

Kurt Bourbaki


1 Answers

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

like image 86
Kevin Bowersox Avatar answered Jun 17 '26 19:06

Kevin Bowersox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!