Count leading zero

How can I get the "series"component to count with leading zeros?

Or how can I achieve leading zeros in a different way without blowing up the definition?

You have to concatenate text and use those numbers as text.

I tried that before but my numbers need to keep the same overall digit count. look what happens with numbers bigger than 9 or 09:

a little python will help:

source: python - Display number with leading zeros - Stack Overflow

1 Like

this will give 0’s before everything. You can do it with the Exprsion component and get the Format you require.

4 Likes

Both work, thanks a lot!
I need it for consisten filename output.

I forgot there is a dedicated component for this now. [Format]

You can read up more on the notation masks here: https://www.grasshopper3d.com/forum/topics/formatting-numbers-in-grasshopper

4 Likes

Even better, thanks a lot!

For some strange reason neither the Format component, nor the Expression component work for me as shown above. Well they did for one brief moment, but now it doesn’t show the leading zero anymore.
So I had to hack this:


I hope it can help somebody.

I do found a better algorithm that can add leading zeros to any length of numbers.

Leading_Zero_new.gh (12.6 KB)

My 2 cents

hi

You should edit multiline your β€œ0” panel

Sorry mate, we are talking here about something totally different thing.

One line of Python:

a = N.zfill(len(L))
2 Likes

Nice one. Unfortunately it works only to generate new numbers but not if you need to add Leading Zeros to existing ones.


zfill_2024Sept17.gh (11.3 KB)

It works exactly as intended, your understanding is flawed.

zfill_2024Sep17b
zfill_2024Sep17b.gh (6.0 KB)

zfill_2024Sep17c
zfill_2024Sep17c.gh (4.6 KB)

You could modify Python to this if you prefer, but β€˜L’ input requires Type Hint β€˜int’:

a = N.zfill(L)

zfill_2024Sep17d
zfill_2024Sep17d.gh (4.9 KB)

1 Like

The idea is to find an algorithm that can fill the necessary leading zeroes AND to adapt automatically to the length of the input data without the need to specify manually the length.

a = N.zfill(L) version:


zfill_2024Sept18.gh (12.3 KB)

My version β€˜Sep17b’ reply yesterday did that and was simpler. :roll_eyes:

1 Like

Oh yeah, looks like I skipped by mistake. Thank you.

If you’re going the pythonic route:

mxlen = len(str(max(N)))

if not prefix: prefix = ""

a = [prefix+str(i).zfill(mxlen) for i in N]


py_zfill.gh (7.3 KB)

1 Like