How to format decimal numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeff_rowa
    New Member
    • Aug 2006
    • 16

    How to format decimal numbers

    Hi guyz,
    could anyone please tell me how can I convert an integer variable like 1 to a string variable but with this format 00X where x is the number, I mean like let's say I want to have a string with fixed size (let's say 3) and I want to convert an int variable into that string but if the int variable is less than 100 I want it to have 0s before that so 1 -> 001 and 10->010

    Regards,
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Look up sprintf and print format specifiers, for you purposes this will work

    Code:
    char string[11];
    int input = 10;
    
    sprintf(string, "%03d", input);

    Comment

    • jeff_rowa
      New Member
      • Aug 2006
      • 16

      #3
      tnx alot :)

      Comment

      Working...