Write an integer to x number of places.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Write an integer to x number of places.

  1. #1
    Join Date
    Nov 2009
    Posts
    29

    Write an integer to x number of places.

    How would I write an integer to a desired number of places. For example, lets say int x = 50; Now, when I write x to a text box it displaces 50, but I want x to display 0050. I could do something simple like "00" + x, but when the number reaches the 100s or 1000s, I do not want the extra zeros.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

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

    Re: Write an integer to x number of places.

    // Convert 'number' to a string and ensure that it
    // is at least 4 characters long by prepending it with
    // zeros if it is shorter than 4.
    int number = GetNumber ();
    string result = number.ToString ().PadLeft (4, '0');
    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.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center