Page 1 of 1

Countdown clock / Integer variable issue

PostPosted: Tue Aug 16, 2011 4:26 pm
by mcavalcanti
Hello again. o/
I built a simple countdown clock that starts in 60 seconds (1 minute). It is working right. My question is the following: is there an easy way to show the numbers with two chars (09, 08, 07...) instead of one char (9, 8, 7...)? Let's suppose the clock started in 100 seconds. In this case, I would like to show numbers with three chars (009, 008, 007...). Thanks.

Re: Countdown clock / Integer variable issue

PostPosted: Tue Aug 16, 2011 4:41 pm
by mcavalcanti
Commenting my own question, should be something like this?

int time;
time=200;
...
t=strlen(time);
if(t<2){sprintf(clock.text, "00%", time);}

Re: Countdown clock / Integer variable issue

PostPosted: Tue Aug 16, 2011 5:14 pm
by mcavalcanti
I put this on a timer:

t=strlen(clock.text);
time-=1;
if(t==2){ sprintf(clock.text, "0%d", time); }
else if(t==1){ sprintf(clock.text, "00%d", time); }
else { sprintf(clock.text, "%d", time); }

But the result was: ... 85, 084, 83, 082, 81, 080, 79, 078, 77, 076...

Re: Countdown clock / Integer variable issue

PostPosted: Tue Aug 16, 2011 9:09 pm
by lcl
Here's how to do it:
Code: Select all
sprintf(clock.text, "%03d", time);

That 03 between the % and d defines how many digits you want to have shown. :)

Re: Countdown clock / Integer variable issue

PostPosted: Tue Aug 16, 2011 9:28 pm
by Game A Gogo
lcl wrote:Here's how to do it:
Code: Select all
sprintf(clock.text, "%03d", time);

That 03 between the % and d defines how many digits you want to have shown. :)


actually only the 3 tells how many digits are to be displayed. the 0 means to fill up with 0's if there is nothing to fill with. If it'd be %3d then it'd fill it up with spaces instead of 0's

Re: Countdown clock / Integer variable issue

PostPosted: Wed Aug 17, 2011 8:33 pm
by lcl
Game A Gogo wrote:
lcl wrote:Here's how to do it:
Code: Select all
sprintf(clock.text, "%03d", time);

That 03 between the % and d defines how many digits you want to have shown. :)


actually only the 3 tells how many digits are to be displayed. the 0 means to fill up with 0's if there is nothing to fill with. If it'd be %3d then it'd fill it up with spaces instead of 0's

Oh, I didn't know that, thanks for pointing that out! :D

Re: Countdown clock / Integer variable issue

PostPosted: Thu Aug 18, 2011 8:18 pm
by mcavalcanti
Beautiful! So simple. :D
Thanks a lot!