0

I would like to format a number in JSTL in ways it will look like:

<c:set var="num" value="12345678000199"/>
<fmt:formatNumber type="number" value="${num}" pattern=?/>
<!-- desired output: 12.345.678/0001-99 (Brazilian CNPJ [companies' IRS ID])-->

Thanks in advance!!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alex
  • 3,325
  • 11
  • 52
  • 80

3 Answers3

2

That's not possible with standard JSTL tags nor with the standard NumberFormat API which it is using under the covers. You'd need to create a custom JSP tag or EL function for that. You could achieve the job with simple String manipulation methods or even (ab)use Swing's MaskFormatter:

String cnpj = "12345678000199";
MaskFormatter mf = new MaskFormatter("##.###.###/####-##");
mf.setValueContainsLiteralCharacters(false);
System.out.println(mf.valueToString(cnpj)); // 12.345.678/0001-99

With an EL function you can ultimately end up like this:

<c:out value="${util:formatCnpj(user.cnpj)}" />

For more detail on creating an EL function, see also the bottom of Hidden features of JSP/Servlet.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

This is not a number. It's a string containing digits.

If you really store such information as a number, you will have to write a formatting method yourself. You could use this formatting method from the JSP by delaring it as a JSP EL function.

Note that if the String can have a leading zero, using a number to store it is an even worse idea.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hi @JB Nizet, thanks for responding. I can see your point, but, for any pourposes, this variable wont ever contain a character other than numbers. It's very common in softwares and applications (even MSOffice) this number formatting in my country. I prefer store this as a number for indexing, relationship, space and performance issues. – Alex Feb 09 '12 at 18:39
  • A double? I could understand a long, but a double? Are you aware that all the numbers, especially when they are very large numbers, can't be represented exactly by a double? – JB Nizet Feb 09 '12 at 18:41
  • No, you're right. I had problems with long in Access, where I have to use doubles to store sufficient large numbers (because its longs couldn't do it). But this is MySQL. – Alex Feb 09 '12 at 18:48
  • Also, you'll regret to have stored them as numbers when your users will ask you to be able to search `cnpj like '12.%/000?-%'` – JB Nizet Feb 09 '12 at 18:52
  • Probably not, because this is a kind of ID which have used to be not sequencial. I can't see a use to "filter" some sequencial CNPJs, because all the companys in the country have one, and I'm going to maintain only few thousands (between millions). The last two digits (99, in the example) are controlers to the number (they're calculated according a kind of mod algorithm). But, you made me ask to myself if would be worthy to query the "/0001", which is a sequencial to some "branch" of a main company (in this case is going to be 0002, 0003, etc.). – Alex Feb 09 '12 at 19:16
0

Using the JSTL code you supplied and the desired result, the pattern to format this is "###.###.###/####'-'##" This assumes that you may have missing leading zeroes. If this is not the case, the pattern can be "000.000.000/0000'-'00"

Make sure you put - in single quotes as it escapes the dash, which is ordinarily a special character (prints - for negative numbers only.

Source: http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm

Tim Sabin
  • 31
  • 1