[OmniFaces utilities] The
formatPercent()
function formats the given number as percentage. This is useful when you want to format numbers as percentage 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(10.5f,
"CO2");
put(0.5f,
"HF");
put(9.0f,
"C6H12O6");
put(50.0f,
"C8H18");
put(30.0f,
"P4O10");
});
public Map<Float, String> getChemistryMap() {
return chemistryMap;
}
Now, we can display a list containing these chemistry numbers. The quantities are formatted as percentage via of:formatPercent() as below:
<h:selectOneListbox style="width:100px;">
<f:selectItems value="#{bookBean.chemistryMap.entrySet()}" var="t"
itemLabel="#{of:formatPercent(t.key)}" itemValue="#{t.value}"/>
itemLabel="#{of:formatPercent(t.key)}" itemValue="#{t.value}"/>
</h:selectOneListbox>
Niciun comentariu :
Trimiteți un comentariu