如何在 JSP 中格式化货币?

jspjava 8object oriented programmingprogramming

<fmt:formatNumber> 标签用于格式化数字、百分比货币

属性

<fmt:formatNumber> 标签具有以下属性 −

属性描述必需Default
ValueNumeric value to displayYesNone
typeNUMBER, CURRENCY, or PERCENTNoNumber
patternSpecify a custom formatting pattern for the output.NoNone
currencyCodeCurrency code (for type = "currency")NoFrom the default locale
currencySymbolCurrency symbol (for type = "currency")NoFrom the default locale
groupingUsedWhether to group numbers (TRUE or FALSE)Notrue
maxIntegerDigitsMaximum number of integer digits to printNoNone
minIntegerDigitsMinimum number of integer digits to printNoNone
maxFractionDigitsMaximum number of fractional digits to printNoNone
minFractionDigitsMinimum number of fractional digits to printNoNone
varName of the variable to store the formatted numberNoPrint to page
scopeScope of the variable to store the formatted numberNopage

示例

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<html>
   <head>
      <title>JSTL fmt:formatNumber Tag</title>
   </head>
   <body>
      <h3>Number Format:</h3>
      <c:set var = "balance" value = "120000.2309" />
      <p>Currency in USA :
         <fmt:setLocale value = "en_US"/>
         <fmt:formatNumber value = "${balance}" type = "currency"/>
      </p>
   </body>
</html>

上述代码将生成以下结果 −

Number Format:
Currency in USA : $120,000.23

相关文章