+4 votes

I need to convert and display number in such a way that it has always taken 2 characters.

Let's say we have a variable "n" which takes values from 0 to 99. If you run:

var n=1

I can get the text to "1" while the need to "01". However, if the variable "n" is >9, the "0" at the front not necessary to add.

How to do it?

in Engine by (356 points)

2 Answers

+9 votes
Best answer
var n=1
by (682 points)
selected by

Seems to be the clearest way to do it, thanks :)

Oh whaaat?! And here I've been constructing number strings digit by digit.

+5 votes
var n = 1
n = "%02d" % [n]
print (n)


by (1,469 points)

It wasn't my question, but thank you so much for the answer! I'm gonna use it.

Don't need to create an array for a single entry, i.e. "%02d" % n should also work.

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.