2

I'm new with grails, I have a g:field for a price input, the problem is that the user can enter a number with comma, for example if user enter 4,23 it will be processed as 423. How can I force to the user enter a dot instead of comma? Or are there some way to convert the comma in the input number to a dot?

This is my grails tag:

<g:field type="number decimal" min="0" maxFractionDigits="2" roundingMode="HALF_DOWN" name="price" value="${fieldValue(bean: productoProveedorInstance, field: 'price')}" required=""/>

Any suggestion will be helpful

fsimon
  • 598
  • 3
  • 6
  • 18
  • some countries actually use commas instead of decimal points for numbers. however, see this http://stackoverflow.com/questions/6178332 – aldrin Mar 22 '16 at 04:50

2 Answers2

1

I succeeded using g:formatNumber.

<g:formatNumber number="${myNumber}" maxFractionDigits="10" locale="US" />

The locale US, forces gsp to use a decimal point. Nevertheless, without maxFractionDigits, it shows a whole number.

Nice Books
  • 1,675
  • 2
  • 17
  • 21
0

Use g:formatNumber instead:

<g:formatNumber number="${myNumber}" format="\\$###.##0" />

...or use pattern attribute for g:field. Here you can find an example for time format.

...or change coma for dot in selected field using javascript.

Community
  • 1
  • 1
Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
  • 1
    I solved this using pattern attribute with a regular expression: `pattern="[0-9]+(\\,[0-9][0-9]?)?"`, g:formatNumber don't aplpy for this case, thks for the advices – fsimon Mar 23 '16 at 11:32
  • That tag `` is for displaying numbers, not formatting fields. – Douglas Mendes Nov 04 '18 at 11:54