How to format a number in Expression Language?
Asked Answered
O

2

14

How can I make a formatted output for a number (e.g. long or BigDecimal) in EL? For example, I want to limit a number of decimal digits to 3 in

${result.returnValue.contract.balance}
Oratorio answered 7/6, 2010 at 21:0 Comment(1)
in most cases you might end up adding both 'maxFractionDigits' and 'minFractionDigits, attributes to get it working.Karly
A
30

Using <fmt:formatNumber/>

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fmt/formatNumber.html

For example:

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

<fmt:formatNumber 
     value="${result.returnValue.contract.balance}" 
     maxFractionDigits="3"/>
Arsenical answered 7/6, 2010 at 21:3 Comment(1)
Upvote, and a quick comment - BigDecimal definitely works like this. If you are having problems, it's not the fault of BigDecimal :)Brinna
H
0

The modern Java EE 5-8 approach with JSF is:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
   <h:outputText value="#{result.returnValue.contract.balance}">
      <f:convertNumber minFractionDigits="3"/>
   </h:outputText> ...

See also Using the Standard Converters. Since Java EE 6 (2009), JSF/Facelets is recommended.

Hopeh answered 30/6, 2018 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.