"%02d" does not work for milliseconds

Godot Version

4.4.1

Question

var time_left:float = time_trial_timer.time_left
	
var minute = floor(time_left / 60)
var second = int(time_left) % 60
var milliseconds = fmod(time_left, 1) * 1000
var time_arr:Array = [minute, second, milliseconds]
	
time_countdown_lbl.text = " " + ("%02d:%02d.%02d" % time_arr) + " "

Above code giving the output below
01:00.230 which is triple digits. minutes and seconds are working fine.

%02d specifies a minimum length to pad with 0s to, I think you mean to use . to round, like %.2f.

%02d gives at least 2 digits of padding with 0, i.e. 01 instead of 1, but your milliseconds number is over 3 digits, 230. You could use %03d instead to always pad up to three digits i.e. 001, or you could calculate centi-seconds instead, I suppose multiplying by 100 instead of 1,000 would do the trick.

Wait the poster did not specify why the formatting wasn’t working. I mean, 230 for milliseconds seems like the correct value since there are 1000 milliseconds in a second.

1 Like

Wanted two digits with no decimals. I mean i just want two digit milliseconds, 02d and .2f not working tried it.
00:23:44 something like that

Two digit milliseconds don’t make sense, since there’s 1,000 milliseconds in a second. When you see something in the format XX:XX:XX it’s actually hours, minutes, and seconds.

1 Like

found the solution in unity forums,
fmod(time_left, 1) * 100
multiplying with 100 instead of 1000 worked.

Sorry, have to disagree about “not making sense” there are many questions in web for this so it is definetely requested feature

You are right, but those are "centi"seconds, not milliseconds, since you rounded to two decimal places

2 Likes