Grails Cookbook - A collection of tutorials and examples

Grails formatNumber Tag Example

The Grails' <g:formatNumber> tag can be used inside GSPs to control the format when displaying numerical values. This is based onJava's DecimalFormat or the JSTL formatNumber tag. Below are some examples on how to use this tag.

Using DecimalFormat Patterns

We may use the format in Java's DecimalFormat class when using <g:formatNumber> . Check the javadoc from Oracle for details. Below are some examples:
<g:formatNumber number="${120544}" format="###,##0" />
<g:formatNumber number="${557181.49}" format="###,##0" />
<g:formatNumber number="${557181.61}" format="###,##0" />
<g:formatNumber number="${120544.48}" format="###,##0.##" />
<g:formatNumber number="${0.1524}" format="%0.##" />

Below are the rendered result for the <g:formatNumber> tag's output:

120,544
557,181
557,182
120,544.48
%15.24 

Padding

We may padd the whole number parts or the decimal part of a number. Below are some examples:
<g:formatNumber number="${12}" type="number" minIntegerDigits="5" />
<g:formatNumber number="${15.05}" type="number" maxFractionDigits="4" />
<g:formatNumber number="${23.22}" type="number" minIntegerDigits="5" maxFractionDigits="4" />
Here is the expected rendered output:
00,012
15.05 
00,023.22 

Rounding

We may also control the nmode used when rounding off numbers in <g:formatNumber> tag. Here are examples on how to use:
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="CEILING" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="DOWN" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="FLOOR" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="HALF_DOWN" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="HALF_EVEN" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="HALF_UP" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="UNNECESSARY" />
<g:formatNumber number="${23.455}" type="number" maxFractionDigits="2" roundingMode="UP" />
Here is the explanation from Oracle's javadoc:
  • CEILING - Rounding mode to round towards positive infinity.
  • DOWN - Rounding mode to round towards zero.
  • FLOOR - Rounding mode to round towards negative infinity.
  • HALF_DOWN - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
  • HALF_EVEN - Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
  • HALF_UP - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
  • UNNECESSARY - Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
  • UP - Rounding mode to round away from zero.

Currency

We may also format a number based on currency. Here are some example usage in <g:formatNumber> :

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

Here is the output of the code above:

$125,500.12
EUR82,553.22 

Available currency codes are based on ISO 4217 standard. Here are the other codes copied from Wikipedia:


CodeCountry
AED United Arab Emirates
AFN Afghanistan
ALL Albania
AMD Armenia
ANG Curaçao (CW),  Sint Maarten (SX)
AOA Angola
ARS Argentina
AUD Australia,  Christmas Island (CX),  Cocos (Keeling) Islands (CC),  Heard Island and McDonald Islands (HM),  Kiribati (KI),  Nauru (NR),  Norfolk Island (NF),  Tuvalu (TV),  Australian Antarctic Territory
AWG Aruba
AZN Azerbaijan
BAM Bosnia and Herzegovina
BBD Barbados
BDT Bangladesh
BGN Bulgaria
BHD Bahrain
BIF Burundi
BMD Bermuda
BND Brunei, auxiliary in  Singapore (SG)
BOB Bolivia
BOV Bolivia
BRL Brazil
BSD Bahamas
BTN Bhutan
BWP Botswana
BYR Belarus
BZD Belize
CAD Canada
CDF Democratic Republic of the Congo
CHE  Switzerland
CHF  Switzerland,  Liechtenstein (LI)
CHW  Switzerland
CLF Chile
CLP Chile
CNY China
COP Colombia
COU Colombia
CRC Costa Rica
CUC Cuba
CUP Cuba
CVE Cape Verde
CZK Czech Republic
DJF Djibouti
DKK Denmark,  Faroe Islands (FO),  Greenland (GL)
DOP Dominican Republic
DZD Algeria
EGP Egypt, auxiliary in  Gaza Strip
ERN Eritrea
ETB Ethiopia
EUR Akrotiri and Dhekelia,  Andorra (AD),  Austria (AT),  Belgium (BE),  Cyprus (CY),  Estonia (EE),  Finland (FI),  France (FR),  Germany (DE),  Greece (GR),  Guadeloupe (GP),  Ireland (IE),  Italy (IT),  Kosovo,  Latvia (LV),  Lithuania (LT),  Luxembourg (LU),  Malta (MT),  Martinique (MQ),  Mayotte (YT),  Monaco (MC),  Montenegro (ME),  Netherlands (NL),  Portugal (PT),  Réunion (RE),  Saint Barthélemy (BL),  Saint Pierre and Miquelon (PM),  San Marino (SM),  Slovakia (SK),  Slovenia (SI),  Spain (ES),   Vatican City (VA); see Eurozone
FJD Fiji
FKP Falkland Islands (pegged to GBP 1:1)
GBP United Kingdom, the  Isle of Man (IM, see Manx pound),  Jersey (JE, see Jersey pound),  Guernsey (GG, see Guernsey pound),  South Georgia and the South Sandwich Islands (GS),  British Indian Ocean Territory (IO) (also uses USD),  Tristan da Cunha (SH-TA), and  British Antarctic Territory
GEL Georgia (except  Abkhazia (GE-AB) and  South Ossetia)
GHS Ghana
GIP Gibraltar (pegged to GBP 1:1)
GMD Gambia
GNF Guinea
GTQ Guatemala
GYD Guyana
HKD Hong Kong,  Macao (MO)
HNL Honduras
HRK Croatia
HTG Haiti
HUF Hungary
IDR Indonesia
ILS Israel,  State of Palestine (PS)[8]
INR India, Bhutan,    Nepal ,  Zimbabwe
IQD Iraq
IRR Iran
ISK Iceland
JMD Jamaica
JOD Jordan, auxiliary in  West Bank
JPY Japan
KES Kenya
KGS Kyrgyzstan
KHR Cambodia
KMF Comoros
KPW North Korea
KRW South Korea
KWD Kuwait
KYD Cayman Islands
KZT Kazakhstan
LAK Laos
LBP Lebanon
LKR Sri Lanka
LRD Liberia
LSL Lesotho
LYD Libya
MAD Morocco
MDL Moldova (except  Transnistria)
MGA Madagascar
MKD Macedonia
MMK Myanmar
MNT Mongolia
MOP Macao
MRO Mauritania
MUR Mauritius
MVR Maldives
MWK Malawi
MXN Mexico
MXV Mexico
MYR Malaysia
MZN Mozambique
NAD Namibia
NGN Nigeria
NIO Nicaragua
NOK Norway,  Svalbard and  Jan Mayen (SJ),  Bouvet Island (BV),  Queen Maud Land,  Peter I Island
NPR   Nepal
NZD New Zealand,  Cook Islands (CK),  Niue (NU),  Pitcairn Islands (PN; see also Pitcairn Islands dollar),  Tokelau (TK),  Ross Dependency
OMR Oman
PAB Panama
PEN Peru
PGK Papua New Guinea
PHP Philippines
PKR Pakistan
PLN Poland
PYG Paraguay
QAR Qatar
RON Romania
RSD Serbia
RUB Russia,  Abkhazia (GE-AB),  South Ossetia,  Crimea
RWF Rwanda
SAR Saudi Arabia
SBD Solomon Islands
SCR Seychelles
SDG Sudan
SEK Sweden
SGD Singapore, auxiliary in  Brunei (BN)
SHP Saint Helena (SH-SH),  Ascension Island (SH-AC) (pegged to GBP 1:1)
SLL Sierra Leone
SOS Somalia (except  Somaliland)
SRD Suriname
SSP South Sudan
STD São Tomé and Príncipe
SYP Syria
SZL Swaziland
THB Thailand ,  Cambodia ,  Myanmar ,  Laos
TJS Tajikistan
TMT Turkmenistan
TND Tunisia
TOP Tonga
TRY Turkey ,  Northern Cyprus
TTD Trinidad and Tobago
TWD Taiwan
TZS Tanzania
UAH Ukraine
UGX Uganda
USD United States,  American Samoa (AS),  Barbados (BB) (as well as Barbados Dollar),  Bermuda (BM) (as well as Bermudian Dollar),  British Indian Ocean Territory (IO) (also uses GBP),  British Virgin Islands (VG), Caribbean Netherlands (BQ - Bonaire, Sint Eustatius and Saba),  Ecuador (EC),  El Salvador (SV),  Guam (GU),  Haiti (HT),  Marshall Islands (MH),  Federated States of Micronesia (FM),  Northern Mariana Islands (MP),  Palau (PW),  Panama (PA),  Puerto Rico (PR),  Timor-Leste (TL),  Turks and Caicos Islands (TC),  U.S. Virgin Islands (VI),  Zimbabwe (ZW),
USN United States
USS United States
UYI Uruguay
UYU Uruguay
UZS Uzbekistan
VEF Venezuela
VND Vietnam
VUV Vanuatu
WST Samoa
XAF Cameroon (CM),  Central African Republic (CF),  Republic of the Congo (CG),  Chad (TD),  Equatorial Guinea (GQ),  Gabon (GA)
XAG
XAU
XBA
XBB
XBC
XBD
XCD Anguilla (AI),  Antigua and Barbuda (AG),  Dominica (DM),  Grenada (GD),  Montserrat (MS),  Saint Kitts and Nevis (KN),  Saint Lucia (LC),  Saint Vincent and the Grenadines (VC)
XDRInternational Monetary Fund
XFUInternational Union of Railways
XOF Benin (BJ),  Burkina Faso (BF),  Côte d'Ivoire (CI),  Guinea-Bissau (GW),  Mali (ML),  Niger (NE),  Senegal (SN),  Togo (TG)
XPD
XPFFrench territories of the Pacific Ocean:  French Polynesia (PF),  New Caledonia (NC),  Wallis and Futuna (WF)
XPT
XSUUnified System for Regional Compensation (SUCRE)[11]
XTS
XUAAfrican Development Bank[12]
XXX
YER Yemen
ZAR South Africa
ZMW Zambia

Tags: formatNumber, Grails Tag