4 Replies Latest reply: Jan 22, 2013 1:01 PM by Dimitar Dimitrov RSS

    Strange problems with <af:convertNumber> component

    user10047839
      Hello,

      I get an exception whenever I try to use the <af:convertNumber> with the following pattern:
             <af:convertNumber groupingUsed="true"
               pattern="{#.##0,###}" type="number"/>
      After all, the format string is the same that could be obtained from:
              DecimalFormat nf = (DecimalFormat)NumberFormat.getNumberInstance();
              DecimalFormatSymbols symbols = nf.getDecimalFormatSymbols();
              nf.setDecimalSeparatorAlwaysShown(true); 
              nf.setGroupingUsed(true);
              symbols.setDecimalSeparator(',');
              symbols.setGroupingSeparator('.');
              nf.setDecimalFormatSymbols(symbols);
               
              String format = nf.toLocalizedPattern();
      The exception is:
      <RegistrationConfigurator> <handleError> ADF_FACES-60096:Server exception during PPR, n. 1
      java.lang.IllegalArgumentException: Malformed pattern "#.##0,###"
           at org.apache.myfaces.trinidad.convert.NumberConverter._getNumberFormatter(NumberConverter.java:1153)
           at org.apache.myfaces.trinidad.convert.NumberConverter._getNumberFormat(NumberConverter.java:1028)
           at org.apache.myfaces.trinidad.convert.NumberConverter.getAsString
      (No properties in trinidad-config.xml have been specified about decimal separators or other format properties as I don't need/want to.)

      Basically I want to swap "," and "." by using "," as grouping separator and "." as a decimal separator, instead of the default format.

      Do anyone know the reason about the above exception? Why does not the converter understand my format?
      This is Jdev 11.1.2.1.0.
        • 1. Re: Strange problems with <af:convertNumber> component
          Dimitar Dimitrov
          The format mask "#.##0,###" is not correct (because it specifies a grouping separator at a location after the decimal point). In Java format masks the symbol "." always refers to a decimal point (e.g. to its position but not to the actual symbol) and the character "," always refers to a grouping separator. The actual symbols (for a decimal point and a grouping separator) that are used when formatting a numeric value and/or parsing numeric literal, depend on the locale. Alternatively, the actual symbols can be overridden using the method DecimalFormat.setDecimalFormatSymbols(...).

          If you want to parse/format your numbers such as the decimal point is parsed/formatted as a comma (",") and grouping separator as a dot ("."), then use the format mask "#,###0.###" and set a proper locale (either in <af:convertNumber locale="..."> or for the whole JSF page in <f:view locale="...">) which specifies the decimal point as a comma and the grouping separator as a dot. An example for such a locale is the Locale( "it", "IT" ).

          Dimitar
          • 2. Re: Strange problems with <af:convertNumber> component
            user10047839
            Thanks for your answer. The problem is how to choose/set the correct Locale then. Suppose I want the page in en_EN but still want the couple ".", "," as separators in the numbers as if they were in the it_IT locale, Would this be possible in some way (without modifying the trinidad-config.xml)? Basically how can I override the actual symbols? I must miss something. I know what setDecimalFormatSymbols does, but how can that modify what the af:convertNumber finally shows? I would really appreciate a simple example.

            Edited by: user10047839 on Jan 22, 2013 10:44 AM
            • 3. Re: Strange problems with <af:convertNumber> component
              BrianV
              You do not need to hack locales.

              Simple create your own converter and then use <af:input converter="com.xyz.numConverter"/>
              • 4. Re: Strange problems with <af:convertNumber> component
                Dimitar Dimitrov
                Declare a managed bean with a name <tt>MyBean</tt> and the following bean class:
                import java.util.Locale;
                public class MyBean {
                  public Locale getCustomNumLocale() {
                    return Locale.ITALY; 
                  }
                }
                Use the following converter:
                <af:convertNumber pattern="#,##0.###" locale="#{MyBean.customNumLocale}"/>
                Dimitar