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

vineri, 4 decembrie 2015

[OmniFaces utilities 2.0] Format the given number in the given pattern


[OmniFaces utilities] The formatNumber() function format the given number in the given pattern. 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 format locale will be set to the one as obtained by Faces#getLocale().

Function:
See also: Faces#getLocale()
Usage:

Let's suppose that we have a map that contains several chemistry numbers, as below:

// in real cases, you may hit a database
private final Map<Float, String> chemistryMap
        = Collections.unmodifiableMap(new HashMap<Float, String>() {
 {
  put(27.734336f, "CO2");
  put(337.2054654f, "HF");
  put(313.783343f, "C6H12O6");
  put(1229.2344f, "C8H18");
  put(3.648f, "P4O10"); }
});

public Map<Float, String> getChemistryMap() {
 return chemistryMap;
}

Now, we can display a list containing these chemistry numbers. The quantities are formatted via of:formatNumber() using two patterns:

// #.000
<h:selectOneListbox style="width:100px;">
 <f:selectItems value="#{bookBean.chemistryMap.entrySet()}" var="t" 
                itemLabel="#{of:formatNumber(t.key, '#.000')}" itemValue="#{t.value}"/>
</h:selectOneListbox>

// 000.0000
<h:selectOneListbox style="width:100px;">
 <f:selectItems value="#{bookBean.chemistryMap.entrySet()}" var="t" 
                itemLabel="#{of:formatNumber(t.key, '000.0000')}" itemValue="#{t.value}"/>
</h:selectOneListbox>

Output:

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors