Sign in to follow this  
Followers 0
golfjrr

Display number with padded zeros

8 posts in this topic

I know this should be simple but alas..

I have a field of say minutes that is 5 but want to force it to be displayed in a string like 03:05:44 with the padded zeros. Now it is showing like 3:5:44.

How do I do that in autoit? I basicall have separate fields for hours, minutes and seconds, and concat them together hh:mi:ss but of course don't get the leading zeros.

The stringformat command has gotta be the most counterintuative/confusing way to format things I have ever seen in my 30+ years of programming..

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

An easy alternative:

If your string it's joined by 3 variables, you could apply a If.

Example:

$var= 10
If $var < 10 Then $var= '0' & $var
Msgbox(0, "", $var)
Edited by josbe

Share this post


Link to post
Share on other sites

StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)

<{POST_SNAPBACK}>

Thank you. I knew it should be simple. I'm not a c programmer and I guess that function/format is taken more from a c function thus it was greek to me. To a c programmer no doubt trival

Share this post


Link to post
Share on other sites

Or you could add a zero to the start regardless of the number and then just do stringright :)

$var = 1
$var = StringRight("0" & $var, 2)
MsgBox(0, "", $var)

<{POST_SNAPBACK}>

Ingenious. :)

Share this post


Link to post
Share on other sites

Even an other idea...

$digits = 3;the number of max digits
while stringlen($var) < $digits
   $var = '0' & $var
wend

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0