1

In an edit.gsp, where I have inputfields for decimal number, the decimal number show up as '3.123' and if I save it: I got error as the decimal point is wrong! It expect a ','. My locale is Sweden. So I have to manually replace dot's with comma for all decimal numbers.

I've looked around the whole week and could not find a solution anywhere. Shouldn't Grails be consistent and show commas when it expect commas in the save? It should work with both BigDecimal and for double.

I have Grails 3.2.4

Here is an example of a "g:field" from an edit-form:

    Bredd: <g:field type="number decimal" name="width" min="20" max="300" required="Y" value="${request1?.width}" style="width: 4em"/>

So, what can I do?

larand
  • 773
  • 1
  • 9
  • 26

1 Answers1

0

manually replacing dots sounds all horrible and possibly the wrong approach.

Moved answer segments around

So an update on the answer since i hit a similar issue today maybe in reverse. Sent through a number of 3333 when validation failed for another reason the number in the field had become 3,333 after the validation failure. If the old validation issue is fixed it will now fail due to comma in the number. The reason turned out to be :

<g:textField value="${fieldValue(bean: instance, field: 'someField')}"

upon return changed number to 3,333 when changing this to

value="${instance.someField}"

Above was actual issue @larand was facing

I would store the input field width as a short

so :

Class MyClass {

 //if you are storing a number like 123 (non decimal)
 Short width
 //if you are storing 12.12 which becomes 1212 when stored
 Integer width

 BigDecimal getDecimalWidth() {
    return new BigDecimal(this.width).movePointRight(2).setScale(2)
 }
 void setWidth(BigDecimal decimal) {
   this.width=new BigDecimal(decimal).movePointLeft(2).setScale(2)
 }
//unsure but think this should work
  Integer getWidthInteger() {
    return this.width as int
 }
 void setWidth(Integer integer) {
   this.width=(byte)integer
 }
}

This will then give you methods to get the short value as big decimal using ${instance.decimalWidth} or as integer : ${instance.widthInteger}

when your field is actually numeric:

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

To me that seems a lot more straight forward and cleaner than chopping up numbers which well you think about it

After first validation issue the number was 3333 as put in. So maybe this is your issue ? Unsure since you are talking of dots

Community
  • 1
  • 1
V H
  • 8,382
  • 2
  • 28
  • 48
  • I don't think you've got me wright. I have a lot of fields in the class that are numeric, both double and BigDecimal, but they all print with a dot. And when I save the form grails don't accept dots, grails want commas. So why do grails print the doubles and bigdecimals with dot's when it doesn't accept dots? Sure something is wrong inside, a bug, a misconfiguration or whatever. Your solution will still give me dot's but I'm still interested what you think this solution would do for me. – larand Feb 05 '17 at 10:22
  • you need to convert the Double number with dots to integer and store then convert back with dots i.e. to bigDecimal for display edit - so you have a two way process. http://stackoverflow.com/questions/4043579/converting-bigdecimal-to-integer. – V H Feb 05 '17 at 12:11
  • you can obviously rely on domain object to magically convert a given type to correct type. If that all works out too complex like above you can create your own manual process during the save process that converts the user input from double to integer or short and then store value. then when displaying reconvert back – V H Feb 05 '17 at 12:20
  • if i had 13.35 and moved 2 points to left it would become 1335 i am now storing a non dot version of that number then when viewing i know i set it back 2 to the left so i show two to the right so it becomes 13.35. I think I understood you clearly first time round, Possibly you didn't quite get what i was trying to get to – V H Feb 05 '17 at 12:46
  • Ok, will give your solution a try and then I'll give you the result later on. – larand Feb 05 '17 at 13:32
  • Your solution didn't work, first there where some casting-problems I think. I tried replacing short with Integer and then I was able to get some data out of it. But then 90 became 900.0 so there is still the dot. I want the decimal delimiter but must be a comma and not a dot. – larand Feb 07 '17 at 20:30
  • apologies about the short express updated answer, it should actually be integer for things like rates. Short if it is a positive number non decimal. If you have it working and it is showing a dot so for example you put in 12.25 and it should now be converted `.movePointLeft(2).setScale(2)` when stored so in the db that should appear as `1225`. When retrieving as it is as an integer it will still be 1225 unless you convert that to a bigdecimal before retrieve – V H Feb 07 '17 at 20:36
  • A comment to your last edit, If you hav the number 3333 and gets it formatted using the "thousand-separator" it is correct to get 3,333 and your decimal point should be a dot. That's the reverse against what we have in Sweden where the "thousand-separator" is a dot and the "Decimal separator" is a comma like: 3.333,33. The problem I have is that grails formats the output with a dot but requires a comma on input that means that it uses the decimal separator differently between output and input. And that must be a bug as I haven't touched anything behind the GSP:s and so on. – larand Feb 07 '17 at 20:42
  • But maybe you have something there. I'll do some test and we will see what happends. – larand Feb 07 '17 at 20:42
  • No, it doesn't help. A value of 90.0 displayed 90.0 before but with your latest example it removed the dot but if I changed it to 90,8 it displayed 90.8 So there must be a bug here or some misconfiguration in the setup of grails-installation. – larand Feb 07 '17 at 21:03
  • I would confirm it by doing a println in the controller before it is returned in gsp what does it show on html output through gsp vs what did controller show it as println. if controller shows correctly then its a formatting issue (possibly not using correct format for number) – V H Feb 07 '17 at 21:13
  • Well, I must have made an mistake in the first attempt b'cause now when I tested with a println in the controller it suddenly worked. So you solved my problem! value="${fieldValue(bean: request1, field: 'width')}" formats the value correct but "${request1?.width}" don't. I think I can live with that. So thank you very much! – larand Feb 07 '17 at 21:38
  • Read my comments from earlier this afternoon, I had the exact same problem in opposite where i didn't want comma's ... :) please accept the answer – V H Feb 07 '17 at 21:47