0

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.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
  • possible duplicate of [How can I change the way GRAILS GSP fieldValue formats Integers?](http://stackoverflow.com/questions/2108255/how-can-i-change-the-way-grails-gsp-fieldvalue-formats-integers) – Victor Sergienko Apr 16 '14 at 07:02

2 Answers2

2

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

Hope this helps.

Scott Warren
  • 1,581
  • 1
  • 17
  • 30
0

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 
Omar Yafer
  • 823
  • 6
  • 17