简体   繁体   中英

java spring jsp model attributes

i have model which contains property:

public BigDecimal amount

when i return it to view its value is setted to 55. in JSP page i have written Amount:

`${amount}`

Result is correct. But i want to divide it to 100 before showing. But i want to do it on jsp only. i am now writing so:

${amount/100} . But result is not correct. it rounds it and result is 1 . how to do it so that it returned 0.55

Try this JSTL

     <fmt:formatNumber pattern="2" value="${amount/100}" var="cellData"/>
${cellData}

One way is to use two attributes

    m.addAttribute("amount", new BigDecimal("55"));
    m.addAttribute("divisor", new BigDecimal("100"));

Then you do this

${amount.divide(divisor)}

Another way is to use a class that extends the BigDecimal , say MyBigDecimal and define a method that does the division.

public class MyBigDecimal extends BigDecimal {

    public MyBigDecimal(String val) {
        super(val);
    }

    public String print() {
        return divide(new BigDecimal("100")).toPlainString();
    }

}

Then you do this.

 m.addAttribute("amount", new MyBigDecimal("55"));
 ${amount.print()}

You can also use custom JSTL functions but I don't fully understand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM