1
votes

I'm facing some problems binding request parameters to a BigDecimal field in a domain class.

When I type in 25.75 in the duration field, the data is serialized correctly and the duration is passed to the controller in the request with correct precision.

Controller action:

def save() {
    // params.duration is 25.75 (debugged and printed to the console)
    def entry = new Entry(params)
    // entry.duration is now 25
    // the precision is lost..
    // 125.25 converts to 125
    // 1.75 converts to 1
    ...
}

The domain class:

class Entry {
    BigDecimal duration

    static constraints = {
        duration(min: 0.01G, max: 168.00G, scale: 2)
    }
}

The column type in MySQL database is DECIMAL(5,2).

Am I missing something apparent?

EDIT: Using Grails version 2.2.0.

1
I just tried this on a new app (Grails 2.2.0) and I'm not seeing this behavior. In the controller the data is being shown correctly both before the save (params.duration) and after the save (entryInstance.duration). Also correct on the list and show pages as well as a direct select from the DB (mysql). Have you tried a grails clean just to check? - Kelly
Kelly, thank you for your input. Yes, I've cleaned and compiled and even tried with a new project. Could the parsing / data binding actually be dependent on the server's locale, somehow? That would seem weird. - Kimi
Could it be your MySQL locale this is the issue? I'm not an expert on MySQL by any means so this may not even make sense. But there does not seem to be anything you are doing wrong with Grails at all. - Kelly

1 Answers

2
votes

What is your locale/browser language? Number parsing is locale dependent. So, if decimal separator for your locale is "," (comma) instead of ".", then you will get integer numbers after data binding (as you described). Try to change your locale to "en" and check again.