Pad number with leading zeros (SOLVED)

I’m using a gui to display the player’s score in my game. It currently just displays the number i.e. “150”. Is there a way to pad the score with leading zeros so it looks like “000150”?

This is the message I’m currently passing to the gui to update the score:

gui.set_text(gui.get_node("score"), message.score)

It’s simple with Lua! Like this;

local padded_score = string.format("%06d", message.score)
gui.set_text(gui.get_node("score"), padded_score)

More info here: https://www.lua.org/pil/20.html

6 Likes

Thank you!
I thought it would be simple but couldn’t find how to do it. I’m only a few days into using Defold and Lua.
Thanks again!

1 Like