CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2006
    Posts
    86

    Display "0034" instead of "34"

    When displaying a number, I want to convert it from an integer to a string with leading zeros. Or something like that.

    So I have an integer. Let's say 34. I want that number to be displayed as 0034. How would I accomplish this?

    EDIT: I'm doing a countdown timer. I wan the timer to count down like this: 12, 11, 10, 09, 08, 07, 06......instead of 12, 11, 10, 9, 8, 7, 6, etc..... So it needs to be dynamically represented, not just adding two 0's to the string.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Display "0034" instead of "34"

    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Dec 2006
    Posts
    86

    Re: Display "0034" instead of "34"

    ah....perfect. Thx


  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Display "0034" instead of "34"

    another option is to use just the 'ToString()' method

    Code:
    int i = 34;
    string s1 = i.ToString("0000");
    string s2 = i.ToString().PadLeft(4, '0');
    They both result in the same

  5. #5
    Join Date
    Dec 2006
    Posts
    86

    Re: Display "0034" instead of "34"

    Yeah, I used the pad left after converting ToString(). Works well for me.

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