Limit the integer and decimal value

HI

I am working on a sample xls form. where I have a limited integer (1-999) can be entered.
I tried this using regex(.,'^[0-9]{1}$') or regex(.,'^[0-9]{2}$') or regex(.,'^[0-9]{3}$') and it works fine.

But I also wanted to add a decimal to this. where as I stick on the value limiitation (0.01 to 999.99)

Could someone help me on this

Thanks

Hi @Giri
can't you just use integer and decimal widgets and add constraint like .>0 and .<1000? If you use regex i guess you used a text widget why?

1 Like

Hi,

Thanks.. Yes I used decimal and limited the values .<1000.
But I would like to limit the decimal values to 2 (0.01, 0.23,999.99)

We don't want to include 998.099 , 12.2324 etc...

Thanks

instead of limiting the number of decimal places you could accept any number of decimal places and then round the result in a calculate question in order to keep your data exports tidy for easy analysis.

alternatively you could write a more complicated regex which forces the response to have 2 decimal places but I am not sure I see why that would be better.

2 Likes

Yeah @noel_cartong is right and the function he mentioned is here https://docs.getodk.org/form-operators-functions/#number-handling

2 Likes

try this:

regex(., '^[0-9]+(\.[0-9]{1,2})?$')

Note, the decimal point is optional, but if you include a decimal point then you must include at least one or two decimal digits [your problem description wasn't specific about this, so I made an assumption...].

2 Likes

I've solve this by using the module function, module functions works wtih integer so you'll have to multiply the decimal input and it gets truncated as an integer. In this case to get 2 decimal numbers use de constraint:
((.*1000 mod 10)=0)

lets see how this works with different inputs:

4
4*1000=4000
(4000 mod 10)=0 valid

4.01
4.01*1000=4010
(4010 mod 10)=0 valid

4.1
4.1*1000=4100
(4100 mod10)=0 valid

4.123
4.123*1000=4123
(4123 mod 10)=3 not valid

I needed to accept only multiples of .05 so I used
((.*1000 mod 50)=0)