String Format padding with zeros

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

    #1

    String Format padding with zeros


    I have strings that are being converted that need to be eight characters
    long. The values are coming in as anywhere between 1 and 8 characters long.
    I need to pad the strings with zeros to make all IDs 8 characters long:

    Example

    Input number Need
    1 00000001
    121 00000121
    10567 00010567


    I tried this but it doesn not work, I simply get the same ID back (Example:
    2)

    sTemp = String.Format(" {0:########}", DR["ID"].ToString());



    What am I missing?

    Thanks,

    Ron


  • Larry Lard

    #2
    Re: String Format padding with zeros


    RSH wrote:[color=blue]
    > I have strings that are being converted that need to be eight characters
    > long. The values are coming in as anywhere between 1 and 8 characters long.
    > I need to pad the strings with zeros to make all IDs 8 characters long:
    >
    > Example
    >
    > Input number Need
    > 1 00000001
    > 121 00000121
    > 10567 00010567
    >
    >
    > I tried this but it doesn not work, I simply get the same ID back (Example:
    > 2)
    >
    > sTemp = String.Format(" {0:########}", DR["ID"].ToString());
    >
    >
    >
    > What am I missing?[/color]

    sTemp = DR["ID"].ToString().Pad Left(8, "0"c);

    --
    Larry Lard
    Replies to group please

    Comment

    • Bruce Wood

      #3
      Re: String Format padding with zeros

      I always find the number formatting stuff difficult to find in the
      documentation, but if you start with String.Format you can eventually
      find it here:

      http://msdn.microsoft.com/library/de...matstrings.asp

      The answer to your question appears to be this format string:

      sTemp = String.Format(" {0:00000000}", DR["ID"].ToString());

      Comment

      • Larry Lard

        #4
        Re: String Format padding with zeros


        Larry Lard wrote:[color=blue]
        > RSH wrote:[color=green]
        > > I have strings that are being converted that need to be eight characters
        > > long. The values are coming in as anywhere between 1 and 8 characters long.
        > > I need to pad the strings with zeros to make all IDs 8 characters long:
        > >
        > > Example
        > >
        > > Input number Need
        > > 1 00000001
        > > 121 00000121
        > > 10567 00010567
        > >
        > >
        > > I tried this but it doesn not work, I simply get the same ID back (Example:
        > > 2)
        > >
        > > sTemp = String.Format(" {0:########}", DR["ID"].ToString());
        > >
        > >
        > >
        > > What am I missing?[/color]
        >
        > sTemp = DR["ID"].ToString().Pad Left(8, "0"c);[/color]

        Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
        course

        sTemp = DR["ID"].ToString().Pad Left(8, '0');

        --
        Larry Lard
        Replies to group please

        Comment

        • Bruce Wood

          #5
          Re: String Format padding with zeros

          Sorry. I didn't notice that ToString() there.

          Assuming that DR["ID"] is a number, not a string, then the code should
          be this:

          sTemp = String.Format(" {0:00000000}", DR["ID"]);

          Comment

          • RSH

            #6
            Re: String Format padding with zeros

            Thanks! Perfect.

            I appreciate the quick response.

            Ron


            "Larry Lard" <larrylard@hotm ail.com> wrote in message
            news:1136915546 .144170.87710@o 13g2000cwo.goog legroups.com...[color=blue]
            >
            > Larry Lard wrote:[color=green]
            >> RSH wrote:[color=darkred]
            >> > I have strings that are being converted that need to be eight
            >> > characters
            >> > long. The values are coming in as anywhere between 1 and 8 characters
            >> > long.
            >> > I need to pad the strings with zeros to make all IDs 8 characters long:
            >> >
            >> > Example
            >> >
            >> > Input number Need
            >> > 1 00000001
            >> > 121 00000121
            >> > 10567 00010567
            >> >
            >> >
            >> > I tried this but it doesn not work, I simply get the same ID back
            >> > (Example:
            >> > 2)
            >> >
            >> > sTemp = String.Format(" {0:########}", DR["ID"].ToString());
            >> >
            >> >
            >> >
            >> > What am I missing?[/color]
            >>
            >> sTemp = DR["ID"].ToString().Pad Left(8, "0"c);[/color]
            >
            > Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
            > course
            >
            > sTemp = DR["ID"].ToString().Pad Left(8, '0');
            >
            > --
            > Larry Lard
            > Replies to group please
            >[/color]


            Comment

            Working...