0 votes

Hi. I have two ints, one is hour and the other one is minutes.
I need to show this ints in a label, formatting them properly. For example, if the hour or minutes int is less than ten (0-> 9) it should be displayed with the corresponding zeroes (00, 01, 02...)
Is there any way to get this numbers formatted?

in Engine by (97 points)

1 Answer

+1 vote
Best answer

I think this is what you want:

func _ready():
    var hour = 6
    var minute = 35
    var time = "%02d:%02d" % [hour, minute]
    print(time)  # prints "06:35"

The %02d will zero-pad each value to be 2 digits wide, adding a leading zero if necessary. See the docs here:

https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_format_string.html

by (10,914 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.