4 Replies Latest reply on Apr 12, 2011 11:28 AM by ginni

    Display Integer as currency?

    ginni

      I have a JSF displaying Integer data types. I want to display the amounts formatted as currency, but assure when the form is submitted that only Integers are submitted.


      I tried adding the f:convertNumber tag, but it does nothing at all.  What is the right way to do this?  If the amount in record.amount is 1000, I want to display $1,000, but when the form is submitted, it must be only 1000 that is submitted.  Thanks!

       

      <h:inputText value="#{record.amount}">

      <f:convertNumber type="currency"/>

      </inputText>

       

       

       

       

       

       

        • 1. Re: Display Integer as currency?
          ppitonak

          Hi,

           

          in my opinion, f:convertNumber is exactly what you need.

           

           

          <h:outputLabel for="currency" value="currency" />
          <h:inputText id="currency" value="#{minBean.value}" label="currency">
              <f:convertNumber type="currency" currencySymbol="€" locale="sk" />
          </h:inputText>
          

           

          My code above renders as follows:

          currency.png

           

          Palo

          • 2. Display Integer as currency?
            ginni

            What is the datatype of your minBean.value? Mine is Integer.  I tried exactly what you have there (with Symbol $) and it is not converted. the Integer 1000 shows just as that, 1000, not $1,000.

            • 3. Display Integer as currency?
              ppitonak

              I tried it with java.lang.Integer, too.

              • 4. Re: Display Integer as currency?
                ginni

                My object has the attribute as Integer:

                 

                 

                private integer amount;

                 

                with the standard getter and setter.

                 

                Then in the UI, I have the code I originally posted in my JSF.  We are using Seam 2.2.1 with RichFaces 3.3.  The amount fields appear in a table as you see below, but they are not formatted like currency, even though the convertNumber tag is there.  The fields appear in a rich:dataTable.  The column code is like so:

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                <h:inputText

                 

                value="#{record.amount}"

                 

                rendered="#{record.amountType and (authorizationUpdatable or !authorizationExists)}"

                 

                validator=

                "numericInputValidator"

                 

                 

                validatorMessage=

                "The #{record.authorizationForm.formTitle} field must be a number"

                 

                 

                styleClass="tableCellInput"

                 

                style="width: 98%"

                >

                 

                 

                <f:convertNumber type="currency" integerOnly="true"/>

                 

                <

                a4j:support

                 

                 

                event="onchange"

                 

                ajaxSingle="true"

                 

                reRender="authorizationToolBar, authorizationControlBar"/>

                 

                </h:inputText

                >

                 

                 

                <h:outputText

                 

                value="#{record.amount}"

                 

                rendered="#{!record.amountType or (!authorizationUpdatable and authorizationExists)}"

                 

                styleClass="tableCellInput"

                 

                style="width: 98%"

                >

                 

                 

                <f:convertNumber type="currency"/>

                </h:outputText>

                 

                But the page renders without formatting.

                 

                amount.gif