padding a numbers string reprensentation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthias S.

    padding a numbers string reprensentation

    Hi,

    I'm pretty sure this is as easy as it gets, but I couldn't find anything
    in the documentation.

    I'd like to change the string representation of an int so that the
    numbers get padded with 0's up to a certain amount of characters: For
    example I'd like to pad up to 4 characters, then a 17 should look like
    this '0017'.

    Any help is greatly appreceated.

    Matthias
  • Bob Grommes

    #2
    Re: padding a numbers string reprensentation

    I don't recall the specifics, but if you look up standard formatting codes
    under String.Format() in the docs you'll find a way to do it with
    someInt.ToStrin g(someFormatStr ingHere).

    Another way would be:

    int intDesiredLengt h = 10;
    string s = someInt.ToStrin g();
    return new String('0',intD esiredLength - intDesiredLengt h) + s;

    --Bob

    "Matthias S." <postamt@emvoid SPAMTRAP.de> wrote in message
    news:eDbOtLdkEH A.592@TK2MSFTNG P11.phx.gbl...[color=blue]
    > Hi,
    >
    > I'm pretty sure this is as easy as it gets, but I couldn't find anything
    > in the documentation.
    >
    > I'd like to change the string representation of an int so that the
    > numbers get padded with 0's up to a certain amount of characters: For
    > example I'd like to pad up to 4 characters, then a 17 should look like
    > this '0017'.
    >
    > Any help is greatly appreceated.
    >
    > Matthias[/color]


    Comment

    • Bob Grommes

      #3
      Re: padding a numbers string reprensentation

      Sorry, I meant:

      return new String('0',intD esiredLength - s.Length) + s;

      --Bob

      "Bob Grommes" <bob@bobgrommes .com> wrote in message
      news:%234RL4Tdk EHA.548@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > I don't recall the specifics, but if you look up standard formatting codes
      > under String.Format() in the docs you'll find a way to do it with
      > someInt.ToStrin g(someFormatStr ingHere).
      >
      > Another way would be:
      >
      > int intDesiredLengt h = 10;
      > string s = someInt.ToStrin g();
      > return new String('0',intD esiredLength - intDesiredLengt h) + s;
      >
      > --Bob
      >
      > "Matthias S." <postamt@emvoid SPAMTRAP.de> wrote in message
      > news:eDbOtLdkEH A.592@TK2MSFTNG P11.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I'm pretty sure this is as easy as it gets, but I couldn't find anything
      > > in the documentation.
      > >
      > > I'd like to change the string representation of an int so that the
      > > numbers get padded with 0's up to a certain amount of characters: For
      > > example I'd like to pad up to 4 characters, then a 17 should look like
      > > this '0017'.
      > >
      > > Any help is greatly appreceated.
      > >
      > > Matthias[/color]
      >
      >[/color]


      Comment

      • Nick

        #4
        Re: padding a numbers string reprensentation

        Lets say you have:
        int i=17;
        string myString = i.ToString("N1" );

        This will give you a string that looks like so. "17.0"
        This is not what you want, but this is just to show you how it works.
        Here is a link that has some other N's so to speak. Try "F4" or something...



        Good luck,
        Nick Z.



        Matthias S. wrote:
        [color=blue]
        > Hi,
        >
        > I'm pretty sure this is as easy as it gets, but I couldn't find anything
        > in the documentation.
        >
        > I'd like to change the string representation of an int so that the
        > numbers get padded with 0's up to a certain amount of characters: For
        > example I'd like to pad up to 4 characters, then a 17 should look like
        > this '0017'.
        >
        > Any help is greatly appreceated.
        >
        > Matthias[/color]

        Comment

        • morgalr

          #5
          Re: padding a numbers string reprensentation


          It's even easier than that:

          string s = iWork.ToString( ).PadLeft(10, '0');

          gives a sting representation of an integer 10 digits long
          padded left with 0's where iWork is just some integer you
          are working with.

          Les
          [color=blue]
          >-----Original Message-----
          >I don't recall the specifics, but if you look up[/color]
          standard formatting codes[color=blue]
          >under String.Format() in the docs you'll find a way to[/color]
          do it with[color=blue]
          >someInt.ToStri ng(someFormatSt ringHere).
          >
          >Another way would be:
          >
          >int intDesiredLengt h = 10;
          >string s = someInt.ToStrin g();
          >return new String('0',intD esiredLength -[/color]
          intDesiredLengt h) + s;[color=blue]
          >
          >--Bob
          >
          >"Matthias S." <postamt@emvoid SPAMTRAP.de> wrote in[/color]
          message[color=blue]
          >news:eDbOtLdkE HA.592@TK2MSFTN GP11.phx.gbl...[color=green]
          >> Hi,
          >>
          >> I'm pretty sure this is as easy as it gets, but I[/color][/color]
          couldn't find anything[color=blue][color=green]
          >> in the documentation.
          >>
          >> I'd like to change the string representation of an int[/color][/color]
          so that the[color=blue][color=green]
          >> numbers get padded with 0's up to a certain amount of[/color][/color]
          characters: For[color=blue][color=green]
          >> example I'd like to pad up to 4 characters, then a 17[/color][/color]
          should look like[color=blue][color=green]
          >> this '0017'.
          >>
          >> Any help is greatly appreceated.
          >>
          >> Matthias[/color][/color]

          Comment

          • James Swindell

            #6
            Re: padding a numbers string reprensentation

            I think the easier implementation would be:

            int i = 17;
            string s = i.ToString().Pa dLeft(4, '0')


            "Matthias S." <postamt@emvoid SPAMTRAP.de> wrote in message
            news:eDbOtLdkEH A.592@TK2MSFTNG P11.phx.gbl...[color=blue]
            > Hi,
            >
            > I'm pretty sure this is as easy as it gets, but I couldn't find anything
            > in the documentation.
            >
            > I'd like to change the string representation of an int so that the
            > numbers get padded with 0's up to a certain amount of characters: For
            > example I'd like to pad up to 4 characters, then a 17 should look like
            > this '0017'.
            >
            > Any help is greatly appreceated.
            >
            > Matthias[/color]


            Comment

            • Vijaye Raji

              #7
              Re: padding a numbers string reprensentation

              string.Format(" {0:0000}", 17)

              would yield "0017"

              -vJ

              "Matthias S." <postamt@emvoid SPAMTRAP.de> wrote in message
              news:eDbOtLdkEH A.592@TK2MSFTNG P11.phx.gbl...[color=blue]
              > Hi,
              >
              > I'm pretty sure this is as easy as it gets, but I couldn't find anything
              > in the documentation.
              >
              > I'd like to change the string representation of an int so that the numbers
              > get padded with 0's up to a certain amount of characters: For example I'd
              > like to pad up to 4 characters, then a 17 should look like this '0017'.
              >
              > Any help is greatly appreceated.
              >
              > Matthias[/color]


              Comment

              Working...