Archived

This topic is now archived and is closed to further replies.

Score Formating.. ex( Converting int 300 into char "00300"

This topic is 6940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, I am working on my highscore system and hit a wall with the formating of the display...Say the score is 220, like most arcade games I would like it to display as 00220 so that every score is the same length. What is the easiest way to convert the score int into a char with the correct number of 0''s in front. I can figure it out but converting the score into 5 different ints - 0, 0, 2, 2, 0 with this: long denominator = 100000; int displayAmmount = gameScore; int digitToDraw; int varread =1; int d1, d2, d3, d4, d5; for( int count = 5; count >= 1; count-- ) { denominator = denominator / 10; if( denominator == 0 ) denominator = 1; digitToDraw = displayAmmount / denominator; displayAmmount = displayAmmount - denominator * digitToDraw; switch (varread) { case(1):{d1=digitToDraw;varread++;break;} case(2):{d2=digitToDraw;varread++;break;} case(3):{d3=digitToDraw;varread++;break;} case(4):{d4=digitToDraw;varread++;break;} case(5):{d5=digitToDraw;varread++;break;} } } But, I am sure that there is a really quick way to do this...anyone have any tips to share? Thanks a lot! -Tim

Share on other sites
you can ue sprintf() to do what you''re asking for.... take a look at one of your compiler''s help files, and you''ll find hout how to do it.... btw, use the %s format with some fillings, to make make all zeroes in front of your numbers, if the score is not large enough....

Share on other sites
ya, that''s a good idea, and the code is just like this:

char scorebuf[8];
sprintf(scorebuf,"%05d",score);

there ya go!

- pouya

Share on other sites
char text[30];int score;sprintf(text, "score: %.5d", score);

the above works too. Can anybody tell me the difference between using the "." instead of the "0" in the sprintf function?

Share on other sites
ya, %.5d will print it with 5 decimal places after the floating point, %05d will print it with 5 decimal places before the floating point and put the leading zero''s there until it reaches the fifth digit. the reason that they are working the same way is that %d (integers) do not have a floating point, so they produce the same results, but technically, %.5d is inproper.

- pouya

• 10
• 13
• 52
• 11
• 15