Connecting Tech Pros Worldwide Forums | Help | Site Map

Using ToString to pad an integer with leading zeroes

stainless
Guest
 
Posts: n/a
#1: Jun 25 '07
Is it possible, using the ToString function, to take an integer and
conver to a string of fiexd width with the leading spaces padded with
zeroes.

e.g. integer 123 converted to a string of length 9 with 6 leading
zeroes i.e. 000000123

integer 98765432 converted to a string of length 9 with 1
leading zero i.e. 098765432

Thus, the string is fixed length with leading zeroes to the right.

Cheers

Mark


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Jun 25 '07

re: Using ToString to pad an integer with leading zeroes


On Jun 25, 9:36 am, stainless <Mark.Wingfi...@gmail.comwrote:
Quote:
Is it possible, using the ToString function, to take an integer and
conver to a string of fiexd width with the leading spaces padded with
zeroes.
>
e.g. integer 123 converted to a string of length 9 with 6 leading
zeroes i.e. 000000123
>
integer 98765432 converted to a string of length 9 with 1
leading zero i.e. 098765432
>
Thus, the string is fixed length with leading zeroes to the right.
(You mean leading zeroes to the left, I believe.)

using System;

class Test
{
static void Main()
{
Console.WriteLine (123.ToString("D9"));
Console.WriteLine (98765432.ToString("D9"));
}
}

Jon

Marc Gravell
Guest
 
Posts: n/a
#3: Jun 25 '07

re: Using ToString to pad an integer with leading zeroes


..ToString("000000000");

Marc


Marc Gravell
Guest
 
Posts: n/a
#4: Jun 25 '07

re: Using ToString to pad an integer with leading zeroes


that is tidier Jon; I was being dumb ;-p


stainless
Guest
 
Posts: n/a
#5: Jun 25 '07

re: Using ToString to pad an integer with leading zeroes


On 25 Jun, 09:45, "Marc Gravell" <marc.grav...@gmail.comwrote:
Quote:
.ToString("000000000");
>
Marc
Thanks,

that looks like the answer

Cheers

stainless
Guest
 
Posts: n/a
#6: Jun 25 '07

re: Using ToString to pad an integer with leading zeroes


On 25 Jun, 09:51, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Quote:
On Jun 25, 9:36 am, stainless <Mark.Wingfi...@gmail.comwrote:
>
Quote:
Is it possible, using the ToString function, to take an integer and
conver to a string of fiexd width with the leading spaces padded with
zeroes.
>
Quote:
e.g. integer 123 converted to a string of length 9 with 6 leading
zeroes i.e. 000000123
>
Quote:
integer 98765432 converted to a string of length 9 with 1
leading zero i.e. 098765432
>
Quote:
Thus, the string is fixed length with leading zeroes to the right.
>
(You mean leading zeroes to the left, I believe.)
>
using System;
>
class Test
{
static void Main()
{
Console.WriteLine (123.ToString("D9"));
Console.WriteLine (98765432.ToString("D9"));
}
>
}
>
Jon
Ok thats an answer too!

Cheers


stainless
Guest
 
Posts: n/a
#7: Jun 27 '07

re: Using ToString to pad an integer with leading zeroes


You will have to excuse my inexperience with C# but I cannot get the
ToString("D9") to work when debugging. The message returned is

No overload for method 'ToString' takes '1' arguments

Should I have defined something earlier in my C# to allow the "D9" as
an argument for this method?

Cheers

Mark

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Jun 27 '07

re: Using ToString to pad an integer with leading zeroes


On Jun 27, 12:50 pm, stainless <Mark.Wingfi...@gmail.comwrote:
Quote:
You will have to excuse my inexperience with C# but I cannot get the
ToString("D9") to work when debugging. The message returned is
>
No overload for method 'ToString' takes '1' arguments
>
Should I have defined something earlier in my C# to allow the "D9" as
an argument for this method?
What are you calling ToString() on?

If you're writing code in the debugger's immediate window, you may
well find it easier to write a very small test application instead (I
almost always use a console app). That way you're isolated from any
differences between the debugger and "normal" code.

Jon

stainless
Guest
 
Posts: n/a
#9: Jun 27 '07

re: Using ToString to pad an integer with leading zeroes


On 27 Jun, 13:02, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Quote:
On Jun 27, 12:50 pm, stainless <Mark.Wingfi...@gmail.comwrote:
>
Quote:
You will have to excuse my inexperience with C# but I cannot get the
ToString("D9") to work when debugging. The message returned is
>
Quote:
No overload for method 'ToString' takes '1' arguments
>
Quote:
Should I have defined something earlier in my C# to allow the "D9" as
an argument for this method?
>
What are you calling ToString() on?
>
If you're writing code in the debugger's immediate window, you may
well find it easier to write a very small test application instead (I
almost always use a console app). That way you're isolated from any
differences between the debugger and "normal" code.
>
Jon
Many apologies. I sorted it out. I was trying to use ToString with a
non integral type (a value returned in an SQL recordset). Once I
converted this to a type "int", the ToString method allowed for
overloads.

Cheers

Mark

Closed Thread