Steve Kuo
Steve Kuo

Reputation: 63134

Grails GSP tab fieldValue formatting

How does the Grails tag fieldValue perform its formatting?

I have an domain class with an Double attribute.

class Thing {
    Double numericValue
}

In GSP, the fieldValue is used (as created by grails generate-all) for rendering:

${fieldValue(bean:thing, field:"numericValue")}

Unfortunately digits after 3 decimal places are not displayed (ie, 0.123456 is displayed as 0.123). How do I control fieldValue's formatting?

Note that I could just use ${thing.numericValue} (which does no formatting) or <g:formatNumber>, but I'd rather use the fieldValue tag and specify the formatting. I just don't know where to specify fieldValue's formatting.

Upvotes: 0

Views: 2620

Answers (2)

Omar Yafer
Omar Yafer

Reputation: 863

An alternative to the answers above is using the i8n files. This option is useful since it can be changed for "All" and depending on the locale

if you go to the messages.properties file you can add the following

default.number.format = ###,##0.00

This will change the default format for all numbers.

If you plan on using the g:formatNumber tag i would suggest using it as

 <g:formatNumber number="${myNumber}" formatName="myCustom.number.format" />

and adding the code entry to the messages.properties files as:

myCustom.number.format = ###,##0.00

by doing this you will only need to use the code wherever you need a similar number format, and, if needed make the changes in a single place.

It would be in your best interests to read this article from the grails docs.


OFFTOPIC: As a side note you can also change the default date format in the messages.properties file as follows

default.date.format=dd 'de' MMMM 'de' yyyy 

Upvotes: 0

Scott Warren
Scott Warren

Reputation: 1611

Use <g:formatNumber number="${thing.numericValue}" format="\\$###,##0.00" /> instead or use ${g.formatNumber(number:thing.numericValue, format:'\\$###,##0.00'}

Hope this helps.

Upvotes: 2

Related Questions