Using if statements in odk collect

Hi all,

I'm trying to reformat a number value to be of the same length.
Here is an example of the expected output
1 = 00001
11 = 00011
111 = 00111
1111 = 01111

I've used the if command below but it doesn't give the output i expect

if((${number}=string-length(1)),concat(0000, ${number}), if((${number}=string-length(2)),concat(000, ${number}), if((${number}=string-length(3)),concat(00, ${number}),concat(0,${number}))))

How can I accomplish this?


I think you can use regex expression, for your case try regex(.,'[0-9]{5}$') - This should force the enumerators to add leading zeros. make sure the question type is text and under appearance you have numbers

Hi @Megykaranja! Welcome to the forum.

For these kinds of form design questions, it's important to design iteratively. Build a very small two question form, and see if you can get it work for 1 = 00001 and 11 = 00011. Once you have that working, incrementally build from there.

From a quick glance at your original post, I think the problems are:

  1. You aren't using string-length() correctly. According to the ODK XForm Spec, it should be string-length(${number}) = 1
  2. You don't have quotes around the concat. I think it should be: concat('0000', ${number})

Try those changes and go from there. Oh and in the future, please post support questions to Support. I've moved the thread.

Thanks so much Yaw.
It worked.

And # support noted.


1 Like