<f:convertNumber>
標簽用于將字符串值轉(zhuǎn)換為所需格式的數(shù)量。以下代碼顯示如何使用<f:convertNumber>
標簽
<f:convertNumber minFractionDigits="2" />
屬性 | 描述 |
---|---|
type | 數(shù)字(默認),貨幣或百分比 |
pattern | 格式化模式,如在java.text.DecimalFormat 中定義的 |
maxFractionDigits | 小數(shù)部分中的最大位數(shù) |
minFractionDigits | 小數(shù)部分中的最小位數(shù) |
maxIntegerDigits | 整數(shù)部分的最大位數(shù) |
minIntegerDigits | 整數(shù)部分的最小位數(shù) |
integerOnly | 如果只解析整數(shù)部分,則為true (默認值:false ) |
groupingUsed | 如果使用分組分隔符,則為false (默認值為true ) |
locale | 用于分析和格式化的首選項的區(qū)域設(shè)置 |
currencyCode | 轉(zhuǎn)換貨幣值時使用的ISO 4217貨幣代碼 |
currencySymbol | 轉(zhuǎn)換貨幣值時使用的貨幣符號 |
以下是文件:index.xhtml 中的代碼 -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:body>
<h:form>
<h:panelGrid columns="3">
Amount :
<h:inputText id="amount" value="#{receipt.amount}"
size="20" required="true"
label="Amount" >
<!-- display in at least 2 decimal points -->
<f:convertNumber minFractionDigits="2" />
</h:inputText>
<h:message for="amount" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
以下是文件:result.xhtml 中的代碼 -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:body>
<p>
Amount [minFractionDigits="2"] :
<h:outputText value="#{receipt.amount}" >
<f:convertNumber minFractionDigits="2" />
</h:outputText>
</p>
<p>
Amount [pattern="#0.000"] :
<h:outputText value="#{receipt.amount}" >
<f:convertNumber pattern="#0.000" />
</h:outputText>
</p>
<p>
Amount [currencySymbol="$"] :
<h:outputText value="#{receipt.amount}">
<f:convertNumber currencySymbol="$" type="currency" />
</h:outputText>
</p>
<p>
Amount [currencyCode="GBP"] :
<h:outputText value="#{receipt.amount}" >
<f:convertNumber currencyCode="GBP" type="currency" />
</h:outputText>
</p>
<p>
Amount [type="percent"] :
<h:outputText value="#{receipt.amount}" >
<f:convertNumber type="percent" />
</h:outputText>
</p>
</h:body>
</html>
以下是文件:UserBean.java 中的代碼 -
package com.yiibai;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="receipt")
@SessionScoped
public class UserBean implements Serializable{
double amount;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
打開 Netbeans創(chuàng)建一個Web工程:ConvertNumber,并使用上面列出的文件代碼。運行項目,在Tomcat啟動完成后,在瀏覽器地址欄中輸入以下URL。
http://localhost:8084/ConvertNumber
如果程序沒有錯誤,應(yīng)該會看到如下結(jié)果 -
在輸入框中填入 999.98
然后提交,應(yīng)該會看到如下結(jié)果 -