473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 46885
..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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.