CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 1999
    Posts
    2

    how to display one digit integer to two or more digit

    I want to display one digit integer to two or more digit.For example,m_hour=1.I want to display '01'.But I don't use complex method.(eg. at first judge the digit of the integer.Then to decide whether add 0 to the number).
    Have you some good methods?Please teach me.
    Thank you all.


  2. #2
    Join Date
    May 1999
    Posts
    37

    Re: how to display one digit integer to two or more digit

    uhhhh... i think if you include "iomanip.h" and use setprecision(x) in your output statement it would format it to whatever(only for console programming).

    #include <iomanip.h>
    ...
    ...
    cout << setprecision(2) << x << endl; // if x==2, outputs '02' ... i think


  3. #3
    Join Date
    May 1999
    Posts
    2

    Re: how to display one digit integer to two or more digit

    I want to use in mfc.


  4. #4
    Join Date
    Apr 1999
    Location
    Philippines
    Posts
    46

    Re: how to display one digit integer to two or more digit

    You could use the CString's Format member function:

    CString sFormattedString;
    // automatically adds leading zero if m_hour is < 10
    sFormattedString.Format("%02d",m_hour);



  5. #5
    Join Date
    May 1999
    Posts
    26

    Re: how to display one digit integer to two or more digit

    use wsprintf(). Is just like printf (you must know printf!, don't you?).


    char outstr[3];
    wsprintf(outstr,"%02d",my_integer);




    and if you want it in a CString just write:


    CString MyString(outstr);




    Then do the output of the string outstr or MyString as you like.

    Again, if you don't understand the "%02d" stuff get a basic C programming book and look for printf's format specifiers.


  6. #6
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: how to display one digit integer to two or more digit

    better to go:


    CString str;
    str.Format( "%02d", m_int );




    the previous will crash if m_int is larger than 99

    Sally


  7. #7
    Guest

    Re: how to display one digit integer to two or more digit

    A "one digit integer" (as jinglei asked) is hardly avobe 99. Besides i was only suggesting to him to use printf's format specifiers.


  8. #8
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: how to display one digit integer to two or more digit

    you never know how source code will develop.....
    I find it a good programming practice to cater for the unforseen

    Sally


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured