java 如何在表达式语言中格式化数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2993129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:51:22  来源:igfitidea点击:

How to format a number in Expression Language?

javajspjstlel

提问by Alexey Kalmykov

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

如何在 EL 中为数字(例如longBigDecimal)进行格式化输出?例如,我想将十进制数字的数量限制为 3

${result.returnValue.contract.balance}

回答by skaffman

Using <fmt:formatNumber/>

使用 <fmt:formatNumber/>

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

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"/>

回答by Marc Dzaebel

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

使用 JSF 的现代 Java EE 5-8 方法是:

<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.

另请参阅使用标准转换器。从 Java EE 6 (2009) 开始,推荐使用 JSF/Facelets。