You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Justyna Horwat <Ju...@Sun.COM> on 2004/05/13 01:50:26 UTC

Re: formatNumber tag

The formatNumber tag uses the DecimalFormat class pattern syntax. Not 
being familiar with the DecimalFormat class, I did some poking around 
and wrote a couple of variations to see how the number is formatted:

----
        DecimalFormat formatter = new DecimalFormat();
        formatter.applyPattern("###-####");
        String phoneNumber = formatter.format(5557176);
        System.out.println(phoneNumber);

        formatter.applyPattern("###'-'####");
        phoneNumber = formatter.format(5557176);
        System.out.println(phoneNumber);

        formatter.applyPattern("###'/-'####");
        phoneNumber = formatter.format(5557176);
        System.out.println(phoneNumber);
       
        formatter.applyPattern("###/-####");
        phoneNumber = formatter.format(5557176);
        System.out.println(phoneNumber);
----

This results in the following output:

----
5557176-
5557176-
5557176/-
5557176/-
----

Looks like you can't use the minus sign as a separater according to the 
DecimalFromat pattern syntax. You can customize the separator using 
DecimalFormat as follows:

----
        DecimalFormatSymbols symbols =
            new DecimalFormatSymbols();
        symbols.setGroupingSeparator('-');
        formatter = new DecimalFormat("###,####", symbols);
        phoneNumber = formatter.format(5557176);
        System.out.println(phoneNumber);
----

Which results in:

----
555-1716
----

The formatNumber tag does not have a mechanism where you can specify 
your own DecimalFormatSymbols with the separator you desire. You could 
write a tag handler that takes in a String and uses that as a separator 
for your String.

Justyna

Karl Coleman wrote:

>I tried and couldn't get it to work. I tried with #'s and zeros. I don't see anything in the DecimalFormat class API that would indicate why it doesn't work the way we expect it to.
>
>Karl
>
>-----Original Message-----
>From: Bharathi Balasubramanian
>[mailto:Bharathi.Balasubramanian@tpwd.state.tx.us]
>Sent: Wednesday, May 12, 2004 10:21 AM
>To: 'taglibs-user@jakarta.apache.org'
>Subject: fmt:formatNumber tag 
>
>
>Hello there, I'm trying to use the fmt:formatNumber tag to format a phone
>number ... does anyone know how to do this?
>
> 
>
>http://www.awprofessional.com/articles/article.asp?p=102219
><http://www.awprofessional.com/articles/article.asp?p=102219&seqNum=5>
>&seqNum=5
>
> 
>
> 
>
><fmt:formatNumber
>  value="${calc.value1 + calc.value2}"
>  pattern="###,###"/>
>
>The pattern indicates that there should be a comma after every three digits.
>It would also be legal to provide a decimal point, as in ###,###.##, which
>would indicate that a comma should be placed every three digits, with two
>digits following the decimal point. 
>
> 
>
> 
>
>The example on this page suggests that this should work:
>
> 
>
><fmt:formatNumber pattern="###-####" value="${phone.phoneNumber}"/>
>
> 
>
> 
>
>But unfortunately that prints out:
>
> 
>
>2228014-
>
> 
>
>Any ideas?
>
> 
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org