Forum for discussing general topics related to Couch.
9 posts Page 1 of 1
Hello boys and girls!

I have a situation here, Users should submit some info and one of the info is a number with 3 decimals.
I have searched all the forum, docs, but without luck. All I have found is displaying/rounding saved numbers, but nothing about submitting numbers, other than 2 digit precizion.
Any idea?

Thanks!
Hi,

If the field is defined with the 'search_type' as 'decimal' then the default for Couch is to save the value with only two decimal places of precision. So, unfortunately, nothing much can be done with that behavior.

That said, depending on how you plan to use the value, you can use a normal 'text' type region and then use a proper validator to make sure the inputted value conforms to the format you desire.

Hope this helps.
It is a tad more intricate than just a validator if done right.

Solution at repository #Snippets → Validator: Regex presents a good code that allows to input flexibly with a validator and formatter:

1.1230 → saved as 1.123 without error
1.1234 → shows "Does not match pattern: 0.000"
.123 → saved as 0.123 without error
.12 → saved as 0.12 without error
KK wrote: Hi,

If the field is defined with the 'search_type' as 'decimal' then the default for Couch is to save the value with only two decimal places of precision. So, unfortunately, nothing much can be done with that behavior.

That said, depending on how you plan to use the value, you can use a normal 'text' type region and then use a proper validator to make sure the inputted value conforms to the format you desire.

Hope this helps.


Thanks for the answer, KK.
That's nothing special about the editable in case, the stored values are only exported in Excel, so a text type region will do the job :) .
trendoman wrote: It is a tad more intricate than just a validator if done right.

Solution at repository #Snippets → Validator: Regex presents a good code that allows to input flexibly with a validator and formatter:

1.1230 → saved as 1.123 without error
1.1234 → shows "Does not match pattern: 0.000"
.123 → saved as 0.123 without error
.12 → saved as 0.12 without error

Sorry for the late response.
I've tested your solution and it's almost perfect, for my use-case. The only problem is that I need exactly 3 decimal precision for the submitted values, so it would be great if that could be achieved. Currently are accepted numbers without decimals, with 1, 2 or 3 decimals. What I really need is exactly 3 decimals, no more, no less.
I think your request can be fulfilled by removing the range and keeping just the 3.
validator='regex=/^(\d+)?(\.\d{1,3})?$/m'

atisz wrote: Currently are accepted numbers without decimals, with 1, 2 or 3 decimals. What I really need is exactly 3 decimals, no more, no less.
trendoman wrote: I think your request can be fulfilled by removing the range and keeping just the 3.
validator='regex=/^(\d+)?(\.\d{1,3})?$/m'


Hm.... It looks like it still accepts numbers without any decimal. I need to force somehow to enter those 3 decimals.

Thanks, trendoman.
Regex is unhumanly, it's so easy to miss something. Please remove another modifier to force digits and have them non-optional.
validator='regex=/^(\d+)?(\.\d{3})?$/m'


atisz wrote:
trendoman wrote: I think your request can be fulfilled by removing the range and keeping just the 3.
validator='regex=/^(\d+)?(\.\d{1,3})?$/m'


Hm.... It looks like it still accepts numbers without any decimal. I need to force somehow to enter those 3 decimals.

Thanks, trendoman.
trendoman wrote: Regex is unhumanly, it's so easy to miss something. Please remove another modifier to force digits and have them non-optional.
validator='regex=/^(\d+)?(\.\d{3})?$/m'


In the above code the period is optional, so I turned it into a mandatory one:
Code: Select all
validator='regex=/^(\d+)\.(\d{3})$/m'

This way it's working as expected.
Thanks trendoman for help and guidance.
9 posts Page 1 of 1