Countdown clock / Integer variable issue

Non-platform specific questions.

Countdown clock / Integer variable issue

Postby mcavalcanti » Tue Aug 16, 2011 4:26 pm

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.
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Countdown clock / Integer variable issue

Postby mcavalcanti » Tue Aug 16, 2011 4:41 pm

Commenting my own question, should be something like this?

int time;
time=200;
...
t=strlen(time);
if(t<2){sprintf(clock.text, "00%", time);}
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Countdown clock / Integer variable issue

Postby mcavalcanti » Tue Aug 16, 2011 5:14 pm

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...
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Countdown clock / Integer variable issue

Postby lcl » Tue Aug 16, 2011 9:09 pm

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. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Countdown clock / Integer variable issue

Postby Game A Gogo » Tue Aug 16, 2011 9:28 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Countdown clock / Integer variable issue

Postby lcl » Wed Aug 17, 2011 8:33 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Countdown clock / Integer variable issue

Postby mcavalcanti » Thu Aug 18, 2011 8:18 pm

Beautiful! So simple. :D
Thanks a lot!
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest