469,328 Members | 1,261 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Using ToString to pad an integer with leading zeroes

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
8 46353
..ToString("000000000");

Marc
Jun 25 '07 #2
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
that is tidier Jon; I was being dumb ;-p
Jun 25 '07 #4
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
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
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
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
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.

Similar topics

1 post views Thread by Mike P | last post: by
2 posts views Thread by tomlebold | last post: by
9 posts views Thread by marcelo27 | last post: by
4 posts views Thread by lenygold via DBMonster.com | last post: by
2 posts views Thread by DanCole42 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.