Limit decimal digits in decimal question

Hi!

I want to limit (constraint) the number of digits that are possible to enter after the point in decimal questions.
Unfortunately I couldn’t find what I want in the Community or Help Center.

I would like to limit to 0 or 1 digit after the point e.g.: 5 or 5.1

I’ve tried with regular expressions but wasn’t successful:
e.g.: regex(.,’^(.{0,1})$’)

Also I understood I could use a Text question type and put Numbers as Appearance but I want to be able to sum the values I obtained, so I guess it’s better to use Decimal questions.

Greetings

Hi @Vico,

Welcome to the community! You could do the same as outlined in the image below:

In the survey tab of your xlsform:

Data entry form (Enketo):

Reference xlsform:
Round.xlsx (9.3 KB)

Have a great day!

2 Likes

Hello,
here the extended example from @Kal_Lam. (You may try with 14.5501 for example)
Round02.xlsx (9.7 KB)
It shows:

Question:
How can we best force a fixed number-conversion of numbers (e.g. for currency), WITH trailing 0?. Like user enters 14.1 >> system will transform to 14.10. Or user enters 14 >> system will transform to 14.00.
Kind regards

1 Like

Many thanks @Kal_Lam and @wroos for your prompt answers.

I know it’s the more scientific and better way to do it.

However, what I really wanted was to constraint when entering the answer and give an error message if a value with more than one decimal digit would be entered.
That way I would avoid the intermediary calculation. Even though I must admit it’s not really a lot of extra work.

But at the same time I would make sure the person entering the value is really mentally present answering the question.

Is there a way to do it or ain’t it possible?

Dear @Kal_Lam and @wroos

I think I finally found something working as I want it :grinning:, at least with the value I tried to enter on Enketo it works.

So, with a Decimal question, as a Validation Criteria/Constraint, I used .<500 and regex(.,’^(\d{0,3})(\.[0-9]{0,1})?$')

In that way, the maximum acceptable value is smaller than 500. It includes that there can be a maximum of 3 digits before the point, which is not really necessary as the maximum is 500, but it didn’t work if I took it away (I’m not familiar enough with regex yet). And it accepts either no or only 1 decimal digit.

Here the XLSForm:
OneDecimalDigit.xls (25 KB)

Do you see any problem with this solution?

Best regards

1 Like

dot (.) is considered a special char in a regex expression. And since you are trying to use it here to represent an actual decimal point, you probably need to escape it; ie

regex(.,’^(\d{0,3})(\.[0-9]{0,1})?$’)

2 Likes

Thanks @Xiphware for making me notice this.
I actually did include the “\” but it wasn’t shown in my message as I didn’t use the “Preformatted text” style. :confused:

I learned something new

1 Like

Hello Kal,
I try to use your example without result.

My syntax is: round(${Salaire_Net},0) div 95 * 100

But the result is always with decimal.
Any idea of my mistake?

Regards

Hello,
it;s fine, together with your . < 500.
regex(.,’^([0-4]{0,1}[0-9]{0,2})(\.[0-9]{0,1})?$')
would cover the same alone.
Trailing zeros will be cut off by Enketo (before the constraint even), you can try with 499,900 or .00 (>> 0)
Best regards

1 Like

Hello,
it seems as you are rounding the Salaire_Net only and THAN div(iding), the latter will create decimals.
Best regards

1 Like

Hi @jmgtoolbox,

as @wroos points out it’s probably due to a mistake in your syntax.

To obtain a rounded final result you have to do the division before the rounding.

In your example it would be round(${Salaire_Net} div 95 * 100, 0)

Hope it solves your problem


En français ça donne:

Si vous voulez que le résultat final soit arrondi il faut d’abord faire la division puis l’arrondir.
En effet, selon votre syntaxe, comme la division n’est pas inclue dans la parenthèse, elle a lieu après. C’est selon les règles de priorités des calculs mathématiques.

Si vous voulez un résultat final arrondi il vous faut l’inclure dans la partie à arrondir, c’est à dire dans la parenthèse, avant le nombre de décimal à inclure.

Votre exemple serait donc: round(${Salaire_Net} div 95 * 100, 0)

En espérant que cela solutionne votre problème.
Meilleures salutations

1 Like

Bonjour,
Hello,

It’s ok and the good solution.

But if i use a more complex operation like:
int(decimal-date-time(${date4}) - decimal-date-time(${date3})+1)1(${PLAFOND_BRUT}) div 30

I try different solution but i have always a syntax error?

VERY YHANK YOU FOR YOUR HELP

JM

Hi,

again, if you want to return an integer (which is a guess as it’s not explained in the question), then you need to include your division in the parenthesis of the int function.

by the way: as I learned myself in a previous post it would be better to write your operation/expression in “Preformatted text” style (see picture) so we don’t loose any part of it.
preformatted_text_surrounded

Best regards

1 Like
Thank you Vico
 Have a  nice Week-end
1 Like