This content has been marked as final.
Show 3 replies
-
1. Re: f:converter and maxdigits
pmuir Sep 4, 2007 12:34 PM (in response to nemya)You probably need to write your own converter that extends either the nubmerconverter or the float converter. This is pretty easy and good tutorials exist.
-
2. Re: f:converter and maxdigits
damianharvey Jan 27, 2008 1:43 AM (in response to nemya)I had a similar problem today but was getting IllegalArgumentExceptions and ClassCastExceptions. For anyone interested below is a basic Float Currency converter. You use it like this:
<h:inputText id="myvalue" converter="#{floatCurrencyConverter}" value="#{mybean.myFloatValue}"/>
import java.text.NumberFormat; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.faces.Converter; import org.jboss.seam.annotations.intercept.BypassInterceptors; @Name("floatCurrencyConverter") @BypassInterceptors @Converter public class FloatCurrencyConverter implements javax.faces.convert.Converter { public Object getAsObject(FacesContext context, UIComponent cmp, String value) { NumberFormat nf = NumberFormat.getCurrencyInstance(); try { return new Float(nf.parse(value).floatValue()); } catch(java.text.ParseException e) { e.printStackTrace(); return null; } } public String getAsString(FacesContext context, UIComponent cmp, Object value) { NumberFormat nf = NumberFormat.getCurrencyInstance(); return nf.format(value); } }
Cheers,
Damian. -
3. Re: f:converter and maxdigits
damianharvey Jan 27, 2008 2:11 AM (in response to nemya)I can't stand Java's implementation of currency formats. What user is ever going to remember to type in the correct currency sign? Better off using the basic format and setting the fractions.
Small change to the above; in both methods replace the:NumberFormat nf = NumberFormat.getCurrencyInstance();
with:NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2);