• Hi all. We have had reports of member's signatures being edited to include malicious content. You can rest assured this wasn't done by staff and we can find no indication that the forums themselves have been compromised.

    However, remember to keep your passwords secure. If you use similar logins on multiple sites, people and even bots may be able to access your account.

    We always recommend using unique passwords and enable two-factor authentication if possible. Make sure you are secure.
  • Be sure to join the discussion on our discord at: Discord.gg/serebii
  • If you're still waiting for the e-mail, be sure to check your junk/spam e-mail folders

C Programming question - number formatting

Misdreavus

used Perish Song!
I'm trying to make a decimal number appear in the exact form of XX:XX. The number is a double representing seconds (while I also have a corresponding integer for minutes), so it's guaranteed to be less than 100, or more specifically, less than 60.

I know that:
  • If x is an integer (printed by %d), I can expand its representation to 2 digits by using %02d. This means that 54 would be printed as 54, 4 would be printed as 04, and 672 would still be printed as 672.
  • If x is a double (printed by %lf), I can expand/contract its decimal representation to 2 digits by using %.2lf. This means that 1 would be printed as 1.00, 1.2 would be printed as 1.20, and 1.23241 would be printed as 1.23.
Knowing this, the logical solution to my problem (at least to me) would be to "combine" both of the above and use %02.2lf, but that doesn't print an extra digit in the "tens" slot (1.2 is still 1.20, not 01.20). Any ideas? I know I could do a conditional statement and just print an extra zero if the double is less than 10 or something like that, but I'd imagine there's a more "professional" way to do it. Thanks!
 
Last edited:
Top