My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 27 februarie 2016

[OmniFaces utilities (2.3)] Format the given number in the default pattern of the given locale


[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
Usage

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:

@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

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors