Www.manorrock.com / Online / Blogs Login
The JSF Converter API | Main | The DateTimeConverter

JSF

The NumberConverter

10/2/2012 12:35 AM
If you are outputting a value, how would you show a currency code along with it?
 <html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
  <h:outputText value="#{user.income}">
   <f:convertNumber currencySymbol="USD" type="currency"/>
  </h:inputText>
 </html>
But in all reality would you not expect a currency symbol instead? How would you show that?
 <html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
  <h:outputText value="#{user.income}">
   <f:convertNumber currencySymbol="$" type="currency"/>
  </h:inputText>
 </html>
Or you need to show the comma's at the thousands, millions and so on.
 <html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
  <h:outputText value="#{user.income}">
   <f:convertNumber currencySymbol="$" groupingUsed="true" type="currency"/>
  </h:inputText>
 </html>
Now changing gears a bit, you want to accept donations. But you want to make sure they can only donate whole numbers.
 <html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
  <h:inputText value="#{user.donation}">
   <f:convertNumber integerOnly="true" type="number"/>
  </h:inputText>
 </html>

Comments

The built-in JSF number converters aren't as useful as they could be: The standard number converter returns numbers as Long or Double. Double is a big no-no when dealing with (large) monetary amounts. There is a converter for BigDecimal, but that one doesn't support I18N. So if you need to support decimal comma you're out of luck. Basically, with respect to number representation JSF has even fallen behind trusty old COBOL (DECIMAL POINT IS COMMA.).

Add a comment
Up

Copyright © 2002-2013 Manorrock.com. All Rights Reserved.