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. formating numbers from strings

formating numbers from strings

Scheduled Pinned Locked Moved C#
databasetutorialquestion
3 Posts 3 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.
  • B Offline
    B Offline
    brsecu
    wrote on last edited by
    #1

    i have a string that always is 6 chars long. If it is not then zeros are added to it. I am pullinging this info out of the database and i need to add 1 to it. Example: I get "000456" and need to add 1 to it so its "000457" Is there an easy way to do that?

    S B 2 Replies Last reply
    0
    • B brsecu

      i have a string that always is 6 chars long. If it is not then zeros are added to it. I am pullinging this info out of the database and i need to add 1 to it. Example: I get "000456" and need to add 1 to it so its "000457" Is there an easy way to do that?

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      Yes, try this:

      string s = "000456";
      int i = -1;
      if (Int32.TryParse(s, out i))
      {
      i +=1;
      s = String.Format("{0:000000}", i);
      }

      This takes your string and tries to convert it to an integer value. If that succeeds, it adds 1 to it and then formats it back as a string to 6 characters padded with zeroes.

      Scott Dorman

      Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


      Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

      1 Reply Last reply
      0
      • B brsecu

        i have a string that always is 6 chars long. If it is not then zeros are added to it. I am pullinging this info out of the database and i need to add 1 to it. Example: I get "000456" and need to add 1 to it so its "000457" Is there an easy way to do that?

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        Another way: int Intvalue = Convert.ToInt32("000456") + 1; string Stringvalue = Intvalue.ToString("000000");


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        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