I needed to format a number with leading zeros, and since the topic has come up a few times on the forum, I thought I’d point to a few techniques as well as offer another variation.
Proposed solutions typically involved using regex or “dummy” elements. Here are a few threads:
- Creascent invoice number (1)
- Creascent invoice number (2)
- Unique number format “00001” instead of 1,2,3
- Format number as 0000N
My approach is most like the last one but does not require any “dummy” elements. Basically, it leverages Bubble’s automatic type conversion when concatenating string literals and numbers within a dynamic expression, and then simply truncates the result to the desired number of characters, thereby preserving only the last portion of the resulting string.
The annoying part is that, for reasons which aren’t clear to me, not all dynamic expression fields within the Bubble editor allow combining string literals with dynamic values. Those that don’t, however, typically DO have a boolean (yes/no) operation available in the list (e.g. “Is development version” or “is not empty”); and the fields for entering the “Formatting for yes/no” DO allow joining string literals with numbers.
So for instance, the following illustrates how one could read a numeric value from a state of type “number” and then set a state of type “text” to the formatted output…
If the value of the “raw_number” state was 37, this would yield the text string “0000037”. No regex and no dummy elements needed!
Needless to say, none of this hoop jumping would be necessary with better built-in number formatting.
Anyway, a very useful technique for circumventing one of Bubble’s idiosyncrasies. Hope it’s helpful.
Thanks to @JohnMark for the tip about the boolean formatting approach!