Search code examples
groovygsp

How to format number with hyphens in gsp?


Currently I have a sortCode 123456 and I would like this thing to be formatted into 12-34-56 in gsp file without using the controller.

Code, where the result is 12,34,56

<g:formatNumber number="${sortCode}" format="##,##" />

Any way to use replaceAll on the above code for example? Or the only way is to use the controller?


Solution

  • Try the following:

    ${g.formatNumber( [number: "${sortCode}", format: "##,##" ] ).replaceAll( ',', '-' )}
    

    The above will fail if sortCode is null/empty so best to protect with <g:if...