[OmniFaces utilities] The
formatNumberDefaultForLocale()
method formats the given number in the default pattern of the given locale. This is useful when you want to format numbers in for example the title
attribute of an UI component, or the itemLabel
attribute of select item, or wherever you can't use the <f:convertNumber>
tag. The given locale can be a Locale
object or a string representation.
Method
We will pass to Numbers#formatNumberDefaultForLocale() a locale of type xx_yy conforming to JDK 8 and JRE 8 Supported Locales, but we will pass a string of type xxx_yy also. The xxx part represents the ISO 639 alpha-2/3 code, and the yy represents the ISO 3166-1 alpha-2country code. For example for Japan we have, jpn_JP. Moreover, we will pass an instance of current Locale:
See also: Utils#parseLocale()
UsageWe will pass to Numbers#formatNumberDefaultForLocale() a locale of type xx_yy conforming to JDK 8 and JRE 8 Supported Locales, but we will pass a string of type xxx_yy also. The xxx part represents the ISO 639 alpha-2/3 code, and the yy represents the ISO 3166-1 alpha-2country code. For example for Japan we have, jpn_JP. Moreover, we will pass an instance of current Locale:
@Named
@RequestScoped
public class AmountBean {
private double amount
= 10242556643.23d;
public double
getAmount() {
return amount;
}
}
// 10,242,556,643.23
<h:outputText value="Amount (Japan): #{of:formatNumberDefaultForLocale(amountBean.amount,
'jpn_JP')}"/>
// 10.242.556.643,23
<h:outputText value="Amount (Romania):
#{of:formatNumberDefaultForLocale(amountBean.amount, 'ro_RO')}"/>
// 10 242 556 643,23
<h:outputText value="Amount (France):
#{of:formatNumberDefaultForLocale(amountBean.amount, 'fr_FR')}"/>
// 10,242,556,643.23
<h:outputText value="Amount (Lule Sami):
#{of:formatNumberDefaultForLocale(amountBean.amount, 'smj')}"/>
// 10,242,556,643.23
<h:outputText value="Amount (your locale):
#{of:formatNumberDefaultForLocale(amountBean.amount, facesContext.viewRoot.locale)}"/>
In practice we can use it whenever <f:convertNumber/> is not an option. For example, we can used for formatting
the value of itemLabel, as
below:
@Named
@RequestScoped
public class AmountBean {
private double
amount;
List<Double>
amounts;
@PostConstruct
public void
init(){
amounts = new
ArrayList<>();
amounts.add(6943.322d);
amounts.add(119033933.2d);
amounts.add(444055493302.22222d);
amounts.add(775.2133d);
amounts.add(21331.2200d);
}
public double
getAmount() {
return amount;
}
public void
setAmount(double amount) {
this.amount = amount;
}
public
List<Double> getAmounts() {
return amounts;
}
}
<h:form>
<h:panelGrid
columns="3">
<h:selectOneMenu
value="#{amountBean.amount}">
<f:selectItems
value="#{amountBean.amounts}" var="t"
itemLabel="#{of:formatNumberDefaultForLocale(t,
'ro_RO')}" itemValue="#{t}"/>
</h:selectOneMenu>
<h:commandButton value="Select
amount"/>
<h:outputText
value="Selected amount (Romania): #{of:formatNumberDefaultForLocale(amountBean.amount,
'ro_RO')}"/>
</h:panelGrid>
</h:form>
Niciun comentariu :
Trimiteți un comentariu