By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

Using ToString to pad an integer with leading zeroes

P: n/a
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

Jun 25 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
..ToString("000000000");

Marc
Jun 25 '07 #2

P: n/a
On Jun 25, 9:36 am, stainless <Mark.Wingfi...@gmail.comwrote:
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

Jun 25 '07 #3

P: n/a
that is tidier Jon; I was being dumb ;-p
Jun 25 '07 #4

P: n/a
On 25 Jun, 09:45, "Marc Gravell" <marc.grav...@gmail.comwrote:
.ToString("000000000");

Marc
Thanks,

that looks like the answer

Cheers

Jun 25 '07 #5

P: n/a
On 25 Jun, 09:51, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Jun 25, 9:36 am, stainless <Mark.Wingfi...@gmail.comwrote:
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
Ok thats an answer too!

Cheers
Jun 25 '07 #6

P: n/a
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

Jun 27 '07 #7

P: n/a
On Jun 27, 12:50 pm, stainless <Mark.Wingfi...@gmail.comwrote:
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

Jun 27 '07 #8

P: n/a
On 27 Jun, 13:02, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Jun 27, 12:50 pm, stainless <Mark.Wingfi...@gmail.comwrote:
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
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

Jun 27 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.