472,783 Members | 991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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 46804
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael . | last post by:
I had an error before involving a temporary table, and that has been taken care of... The last message I wrote where it seemed to have needed it after I added it was because of different...
1
by: Jean-michel | last post by:
I need to convert a decimal field to char() but also trim the leading zeroes. Any idea? I could not find any function to do that.
1
by: Mike P | last post by:
I'm trying to write the contents of a csv file to a table, but I am having problems with fields with leading zeroes. Whenever I save as csv I lose the leading zeroes. Does anybody know how to...
2
by: tomlebold | last post by:
How do you remove leading zeroes in a column? Update 0000123456 to 123456
9
by: jbaranski | last post by:
Access 2003 on XP pro machine... i'm running a crosstab query and an export to a 3rd party company showing different benefit plans for employees; specifically dental, vision and medical pulling...
9
by: marcelo27 | last post by:
I need to add leading zeroes to an input box. For example, the user enters "2" , I need to convert to a three digit number eg."002". If the user enters "23", I need to convert to "023" If the user...
2
by: ahalf0rd | last post by:
Hi, I have a text field as a unique id number for my database. It is in the format CP????? where the first record will be CP00001, CP00002 etc. Unfortunately I cannot get my table or form to...
4
by: lenygold via DBMonster.com | last post by:
I have the following column: 1.4.1 1.10.1 1.10.1.2.2 1.22.99.1 2 2.8.11 2.7.7
2
by: DanCole42 | last post by:
Newbie question, here. I have a database that frequently imports ZIP code data that frequently needs cleaning: 12365 6487 64684-3543 3213-6546
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.