Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to format an integer into a string?

How to format an integer into a string?

Scheduled Pinned Locked Moved C#
questioncsharpc++tutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larry Antram
    wrote on last edited by
    #1

    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!

    H J 2 Replies Last reply
    0
    • L Larry Antram

      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!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You should read the documentation for the NumberFormatInfo class and the String.Format method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would use Console.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-----

      L J 2 Replies Last reply
      0
      • H Heath Stewart

        You should read the documentation for the NumberFormatInfo class and the String.Format method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would use Console.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-----

        L Offline
        L Offline
        Larry Antram
        wrote on last edited by
        #3

        Thanks! I was having a hard time trying to find the info in the documentation.

        H 1 Reply Last reply
        0
        • L Larry Antram

          Thanks! I was having a hard time trying to find the info in the documentation.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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-----

          1 Reply Last reply
          0
          • L Larry Antram

            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!

            J Offline
            J Offline
            J Dunlap
            wrote on last edited by
            #5

            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.'

            FLUID UI Toolkit | FloodFill in C# & GDI+**

            1 Reply Last reply
            0
            • H Heath Stewart

              You should read the documentation for the NumberFormatInfo class and the String.Format method. You still can use format specifiers, but they work a little differently. For a similar format for "%03d", you would use Console.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-----

              J Offline
              J Offline
              J Dunlap
              wrote on last edited by
              #6

              Ahh... you always beat me to it! ;)

              **"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson

              FLUID UI Toolkit | FloodFill in C# & GDI+**

              H N 2 Replies Last reply
              0
              • J J Dunlap

                Ahh... you always beat me to it! ;)

                **"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson

                FLUID UI Toolkit | FloodFill in C# & GDI+**

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                :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-----

                1 Reply Last reply
                0
                • J J Dunlap

                  Ahh... you always beat me to it! ;)

                  **"Peace cannot be achieved through violence, it can only be attained through understanding." -- Ralph Waldo Emerson

                  FLUID UI Toolkit | FloodFill in C# & GDI+**

                  N Offline
                  N Offline
                  Nick Seng
                  wrote on last edited by
                  #8

                  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

                  J 1 Reply Last reply
                  0
                  • N Nick Seng

                    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

                    J Offline
                    J Offline
                    J Dunlap
                    wrote on last edited by
                    #9

                    :-D

                    **"War is cruelty, and you cannot refine it." -- Gen. William T. Sherman

                    FLUID UI Toolkit | FloodFill in C# & GDI+**

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups