Search code examples
grailsgroovyiterationgrails-ormnumber-formatting

Groovy/Grails iterating the list and formatting the number using NumberFormat(US style)


I have a Employee domain class and properties name,age,salary,annualPackage. When I call Employee.list(). All the properties will be persisted.

Now I want to apply numberformat for salary and annualPackage properties.I can achieve it by:

NumberFormat.getNumberInstance(Locale.US).format(Employee.salary.get(0))

But How to format all the values of salary and annualPackage inside the list.


Solution

  • This should do it

    def formatter = java.text.NumberFormat.getNumberInstance(Locale.US)
    def formattedValues = Employee.list().collect { 
      [
        salary = formatter.format(it.salary)
        annualPackage = formatter.format(it.annualPackage)
      ]
    }