Formatting Numbers/Leading zeros.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Code::Tyr

    Formatting Numbers/Leading zeros.

    Hello. I have a simple problem and need help. I am developing a simple
    file system that stores info based on folder names. Now, I only have
    one problem. How do i format (int i = 2) into a string like "00002"
    instead of the defualt "2"?

  • rossum

    #2
    Re: Formatting Numbers/Leading zeros.

    On 7 Jan 2007 10:13:16 -0800, "Code::Tyr" <leon1jung@gmai l.comwrote:
    >Hello. I have a simple problem and need help. I am developing a simple
    >file system that stores info based on folder names. Now, I only have
    >one problem. How do i format (int i = 2) into a string like "00002"
    >instead of the defualt "2"?
    Console.WriteLi ne("Like this: {0:D5}", 2);

    alternatively:

    Console.WriteLi ne("Like this: " + 2.ToString("000 00"));

    rossum

    Comment

    • boston_irish@cox.net

      #3
      Re: Formatting Numbers/Leading zeros.


      Code::Tyr wrote:
      Hello. I have a simple problem and need help. I am developing a simple
      file system that stores info based on folder names. Now, I only have
      one problem. How do i format (int i = 2) into a string like "00002"
      instead of the defualt "2"?
      private static string AppendZerosBegi nning(int input, int length)
      {
      return input.ToString( ).PadLeft(lengt h, Convert.ToChar( "0"));
      }

      If the numeric values are coming out of SQL Server, you can also use
      the REPLICATE keyword in your select statement to pad the int with
      leading zeros:



      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Formatting Numbers/Leading zeros.

        boston_irish@co x.net wrote:
        Code::Tyr wrote:
        >Hello. I have a simple problem and need help. I am developing a simple
        >file system that stores info based on folder names. Now, I only have
        >one problem. How do i format (int i = 2) into a string like "00002"
        >instead of the defualt "2"?
        >
        private static string AppendZerosBegi nning(int input, int length)
        {
        return input.ToString( ).PadLeft(lengt h, Convert.ToChar( "0"));
        }
        There are already posted much better solutions.

        Arne

        Comment

        • Stephany Young

          #5
          Re: Formatting Numbers/Leading zeros.

          How about a variation:

          private static string AppendZerosBegi nning(int input, int length)
          {
          return input.ToString( new string("0", length));
          }


          <boston_irish@c ox.netwrote in message
          news:1168279017 .398183.200440@ 42g2000cwt.goog legroups.com...
          >
          Code::Tyr wrote:
          >Hello. I have a simple problem and need help. I am developing a simple
          >file system that stores info based on folder names. Now, I only have
          >one problem. How do i format (int i = 2) into a string like "00002"
          >instead of the defualt "2"?
          >
          private static string AppendZerosBegi nning(int input, int length)
          {
          return input.ToString( ).PadLeft(lengt h, Convert.ToChar( "0"));
          }
          >
          If the numeric values are coming out of SQL Server, you can also use
          the REPLICATE keyword in your select statement to pad the int with
          leading zeros:
          >

          >

          Comment

          • Code::Tyr

            #6
            Re: Formatting Numbers/Leading zeros.

            Thanks, all!

            Stephany Young wrote:
            How about a variation:
            >
            private static string AppendZerosBegi nning(int input, int length)
            {
            return input.ToString( new string("0", length));
            }
            >
            >
            <boston_irish@c ox.netwrote in message
            news:1168279017 .398183.200440@ 42g2000cwt.goog legroups.com...

            Code::Tyr wrote:
            Hello. I have a simple problem and need help. I am developing a simple
            file system that stores info based on folder names. Now, I only have
            one problem. How do i format (int i = 2) into a string like "00002"
            instead of the defualt "2"?
            private static string AppendZerosBegi nning(int input, int length)
            {
            return input.ToString( ).PadLeft(lengt h, Convert.ToChar( "0"));
            }

            If the numeric values are coming out of SQL Server, you can also use
            the REPLICATE keyword in your select statement to pad the int with
            leading zeros:

            Comment

            Working...