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


luni, 30 noiembrie 2015

[OmniFaces utilities 2.0] Format the given number as currency with the given symbol

[OmniFaces utilities] The formatCurrency() function format the given number as currency with the given symbol. This is useful when you want to format numbers as currency 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().

See also: Faces#getLocale()

Let's suppose that we have a map that contains several JSF books names and their prices as floats, as below:

// in real cases, you may hit a database
private final Map<Float, String> booksMap
        = Collections.unmodifiableMap(new HashMap<Float, String>() {
  put(27.76f, "Mastering JavaServer Faces 2.2");
  put(37.20f, "Mastering OmniFaces");
  put(33.78f, "JavaServer Faces 2.2");
  put(29.44f, "JavaServer Faces 2.0, The Complete Reference");
  put(38.68f, "Pro JSF and HTML5");

public Map<Float, String> getBooksMap() {
 return booksMap;

Now, we can display a table containing these books names and prices. The prices are formatted via of:formatCurrency() using $ symbol:

<h:dataTable value="#{bookBean.booksMap.entrySet()}" var="book">
  <f:facet name="header">Book</f:facet>
  <f:facet name="header">Price</f:facet>
  #{of:formatCurrency(book.key, "$")}

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors