A custom number to make a unique ID

Hi, Ahmed

I'm not aware of a way to directly pad a number with zeros, but you can make it work with some creativity. This should do what you want for any number between 1 and 99,999:

concat(substr(string(#form/number*0.00001), 2, 7), #form/code)

Here's how that works:

  • #form/number0.00001 moves the decimal place 5 digits over so you get the correct padding. For example, 1250.00001 is 0.00125
  • string(#form/number*0.00001) converts that to a string so you can use string manipulation functions on it
  • substr(string(#form/number*0.00001), 2, 7) extracts the 5 digits after the decimal place. For example substr("0.00125", 2, 7) is 00125
  • And concat is used to attach the code to the end.

Note also that there is a wiki page on generating unique IDs that you may find useful:

https://wiki.commcarehq.org/display/commcarepublic/Generating+a+Unique+ID+for+beneficiaries