Allows the formatting of numbers using the same patterns defined by the DecimalFormat class. Supports also attributes that are used in the JSTL formatNumber tag.


<g:formatNumber number="${myNumber}" format="\\$###,##0" />

Example of formatting an EUR currency amount:

<g:formatNumber number="${myCurrencyAmount}" type="currency" currencyCode="EUR" />

Example of formatting a USD currency amount:

<g:formatNumber number="${myCurrencyAmount}" type="currency" currencyCode="USD" />
For USD, the currency symbol will be based on the locale of the request. For the "en_US" locale, the result is "$123.45" and for "fi_FI" locale, the formatting is "123,45 USD". The formatting rules come from the java.util.Currency class.

Example of left padding a number with zeros:

<g:formatNumber number="${myNumber}" type="number" minIntegerDigits="9" />

Example of formatting a number showing 2 fraction digits:

<g:formatNumber number="${myNumber}" type="number" maxFractionDigits="2" />

Example of formatting a number showing 2 fraction digits, rounding with RoundingMode.HALF_DOWN:

<g:formatNumber number="${myNumber}" type="number" maxFractionDigits="2" roundingMode="HALF_DOWN" />




Show Source