How to format an integer into a string?
-
How can I format an integer into a string so that it is a fixed size (with leading zeros if necissary)? In C++, I could do something like this:
int val = 1;
printf( "%03d", val );And it would appear as "001" on the screen. In C#, I can do something like this:
int val = 1;
Console.WriteLine( "{0}", val );But I can't figure out how to format the "{0}" part so that what is printed on the screen appears as "001" instead of just "1". Any assistance is greatly appreciated!
-
How can I format an integer into a string so that it is a fixed size (with leading zeros if necissary)? In C++, I could do something like this:
int val = 1;
printf( "%03d", val );And it would appear as "001" on the screen. In C#, I can do something like this:
int val = 1;
Console.WriteLine( "{0}", val );But I can't figure out how to format the "{0}" part so that what is printed on the screen appears as "001" instead of just "1". Any assistance is greatly appreciated!
You should read the documentation for the
NumberFormatInfo
class and theString.Format
method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would useConsole.WriteLine("{0:000}", val)
. This more closely represents the MaskEdit control commonly used in VB (and various ports).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
You should read the documentation for the
NumberFormatInfo
class and theString.Format
method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would useConsole.WriteLine("{0:000}", val)
. This more closely represents the MaskEdit control commonly used in VB (and various ports).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks! I was having a hard time trying to find the info in the documentation.
-
Thanks! I was having a hard time trying to find the info in the documentation.
Don't worry. That little feature isn't actually documented, just like using a number after the "X" format code to manipulate hex values, like "X2" is used to format a byte so that you always get 2 characters per byte (necessary for hex values in many cases). I meant to mention that it isn't really documented in the .NET Framework SDK, but I only remember after I hit "Submit".
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
How can I format an integer into a string so that it is a fixed size (with leading zeros if necissary)? In C++, I could do something like this:
int val = 1;
printf( "%03d", val );And it would appear as "001" on the screen. In C#, I can do something like this:
int val = 1;
Console.WriteLine( "{0}", val );But I can't figure out how to format the "{0}" part so that what is printed on the screen appears as "001" instead of just "1". Any assistance is greatly appreciated!
int val = 1;
Console.WriteLine( "{0,0:000}", val );**But the angel said to them, '...I bring you good news of great joy... Today in the town of David a Savior has been born to you; he is Christ the Lord.'
-
You should read the documentation for the
NumberFormatInfo
class and theString.Format
method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would useConsole.WriteLine("{0:000}", val)
. This more closely represents the MaskEdit control commonly used in VB (and various ports).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Ahh... you always beat me to it! ;)
**"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson
-
Ahh... you always beat me to it! ;)
**"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson
:jig::)
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Ahh... you always beat me to it! ;)
**"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson
-
Ah, Justin, shouldn't you know by now that there's no beating Heath! ;)
"if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler. Support Bone
:-D
**"War is cruelty, and you cannot refine it." -- Gen. William T. Sherman