Custom number formatting in JSP with JSTL for a given locale

So this one was pretty painful to resolve and I spent a few hours struggling with it.. especially since I haven't work with JSP for A WHILE...

Usually when you need to format a given number (in my case a BigDecimal) you can use the fmt:formatNumber tag if you have setted your locale properly it should use the appropriate decimal separator according to the language.

However this time it wasn't working and I'm really not sure why, other fields in which I was using the spring tag spring:bind the output was properly formatted

I needed to output a large number using grouping separators (by groups of 3) and at least 2 decimals

What I was trying to accomplish was to have a number with Swiss grouping and decimal separators like this 2'300'120.40 but I was getting this 2 300 120,40

I tried a lot of things, like setting the locale for the page using the setLocaletag


   

Forcing the pattern when calling the formatNumber function


   

And different combinations of values for the different attributes of the formatNumber tag, such as type="currency" nothing worked!! and the spring:bind just stood there gloating !!

It would have been great if the formatNumbertag would allow you to force the grouping and decimal character that would be used, but it's just not possible.

Luckily however this can be done in Java using the java.text.DecimalFormat ; and java.text.DecimalFormatSymbols

Since I was running out of time the workaround that I found was :

  1. Create a Util class in Java with a static method that would do the formatting and conversion how I wanted it to be done
  2. Declare this static method as a custom JSTL tag
  3. Call this function with my number as argument to obtain the formatted String
  4. Go enjoy a coffee :)

Below are the code snippets I used

The Java util method :


  public static String bigDecimalToFormattedString(BigDecimal bd)
  {
    String formattedString = null;
    try
    {
      DecimalFormatSymbols symbols = new DecimalFormatSymbols();
      symbols.setDecimalSeparator('.');
      symbols.setGroupingSeparator('\'');
      
      String strange = "###,###.##";
      
      DecimalFormat chCurrencyFormat = new DecimalFormat(strange, symbols);
      chCurrencyFormat.setGroupingSize(3);
      chCurrencyFormat.setMinimumFractionDigits(2);
      
      formattedString = chCurrencyFormat.format(bd);
    }
    catch (Exception e)
    {
     logger.error(String.format("Unable to convert BigDecimal : %s to String. error : %s", new Object[] { bd, e.getMessage() }));
    }
    return formattedString;
  }

You will need then to declare your custom functions in a .tld file that needs to be placed under the WEB-INF :




    Custom Functions    
    1.0
    http://java.sun.com/xml/ns/javaee:cf

    
      Converts a given BigDecimal to a formatted String with decimal and grouping separators
        bd2s
        com.ufasoli.StringUtils
        String bigDecimalToFormattedString(java.math.BigDecimal)      
    

Once this is done you can use your custom functions by simply importing the taglib in a JSP file


<%@taglib prefix="cf" uri="http://java.sun.com/xml/ns/javaee:cf"  %>
.....
......

Now all of this can certainly be improved like by allowing the user to override the pattern, or the grouping separator, but in my case the output would be the same formatting throughout the application so I didn't need them. Regarding the hyper generic I know it's a really bad practice and should not be used but in this case we needed a quick and dirty solution that will not cause the app to fail if an exception occurred when formatting a given number

Popular posts from this blog

Multi-hop ssh tunnel - howto : Creating a SSH tunnel with port forwarding between multiple hosts

Spring boot and spring data jpa tutorial - A sample application using spring 4.0 spring boot and JPA (part 2/2)

MongoDB spring data $elemMatch in field projection