Formatting Number With Leading Zeros (Zero-Padding)

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:

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!


Thanks @sudsy for another helpful post!

1 Like


This is genius! Thanks a lot for that.

This helped a lot! Thank you very much!

Maybe a silly question depending on the use case:
Why not store the number as normal then just pad it with zero’s at output time via UI constructs?

I’m a bit confused since that’s exactly what this tip does. The number is stored as a number type and then converted to a string with leading zeros for output / display.

The :formatted as operator for numbers (which also converts the number to a string) does not support leading zeros - hence the need for a work-around.

Sorry I did not pick up on that… when I left pad numbers with zero’s for output I simply do (for example):
Arbitrary text - append - number - truncated from end to 5.
Where Arbitrary text = “0000”.

Yes, it’s basically the same thing (conceptually), but please note that the initial post here was made over 3 years ago when there was no Arbitrary text operator - hence the additional shenanigans.

I do something similar now as well. My practices are constantly evolving with the Bubble platform.

Of course, the best solution for this particular problem would be for Bubble to support leading zeros when formatting a number. :slightly_smiling_face:

1 Like

Hi, I actually liked your 3 year old approach; even though bubble evolves and we adapt.
I thought it was very creative, and I always enjoy different approaches to problems because you never quite know when collectively we will stumble on that one moment of genius. Hope you did not consider my posts as being critical, I just enjoy interacting with others on the forum. I always learn something.
All the best, John

1 Like

Brilliant! Solved in a moment. Many thanks!