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.
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
Re: how to display one digit integer to two or more digit
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);
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.
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
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.
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