3 Replies Latest reply on Jan 27, 2008 2:11 AM by Damian Harvey

    f:converter and maxdigits

    Aymen Newbie

      Hi,
      I have an inputtext for a float value. I added the converter to have this:

      <h:inputText value="#{MyBean.MyValue}">
       <f:converter converterId="javax.faces.Float" />
      </h:inputText>


      This works fine but I'd like this float value to be displayed with 2 fraction digits. I know that the <f:convertNumber> with the maxFractionDigits and minFractionDigits does this but when I use it like this :
      <h:inputText value="#{MyBean.MyValue}">
       <f:converter converterId="javax.faces.Float" />
       <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>
      </h:inputText>


      I have a cast exception
      java.lang.ClassCastException: java.lang.Double


      And when I use it like this:
      <h:inputText value="#{MyBean.MyValue}">
       <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>
       <f:converter converterId="javax.faces.Float" />
      </h:inputText>


      I have no problem but I don't have 2 fraction digits.

      Some help please! (using Jboss Seam 1.2.1.GA + Jboss AS 4.0.5.GA)

        • 1. Re: f:converter and maxdigits
          Pete Muir Master

          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
            Damian Harvey Master

            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
              Damian Harvey Master

              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);