473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Formatting Numbers/Leading zeros.

Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?

Jan 7 '07 #1
5 27694
On 7 Jan 2007 10:13:16 -0800, "Code::Tyr" <le*******@gmail.comwrote:
>Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?
Console.WriteLine("Like this: {0:D5}", 2);

alternatively:

Console.WriteLine("Like this: " + 2.ToString("00000"));

rossum

Jan 7 '07 #2

Code::Tyr wrote:
Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?
private static string AppendZerosBeginning(int input, int length)
{
return input.ToString().PadLeft(length, Convert.ToChar("0"));
}

If the numeric values are coming out of SQL Server, you can also use
the REPLICATE keyword in your select statement to pad the int with
leading zeros:

http://sqlblogcasts.com/blogs/tonyro...05/29/765.aspx

Jan 8 '07 #3
bo**********@cox.net wrote:
Code::Tyr wrote:
>Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?

private static string AppendZerosBeginning(int input, int length)
{
return input.ToString().PadLeft(length, Convert.ToChar("0"));
}
There are already posted much better solutions.

Arne
Jan 9 '07 #4
How about a variation:

private static string AppendZerosBeginning(int input, int length)
{
return input.ToString(new string("0", length));
}
<bo**********@cox.netwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
>
Code::Tyr wrote:
>Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?

private static string AppendZerosBeginning(int input, int length)
{
return input.ToString().PadLeft(length, Convert.ToChar("0"));
}

If the numeric values are coming out of SQL Server, you can also use
the REPLICATE keyword in your select statement to pad the int with
leading zeros:

http://sqlblogcasts.com/blogs/tonyro...05/29/765.aspx

Jan 9 '07 #5
Thanks, all!

Stephany Young wrote:
How about a variation:

private static string AppendZerosBeginning(int input, int length)
{
return input.ToString(new string("0", length));
}
<bo**********@cox.netwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...

Code::Tyr wrote:
Hello. I have a simple problem and need help. I am developing a simple
file system that stores info based on folder names. Now, I only have
one problem. How do i format (int i = 2) into a string like "00002"
instead of the defualt "2"?
private static string AppendZerosBeginning(int input, int length)
{
return input.ToString().PadLeft(length, Convert.ToChar("0"));
}

If the numeric values are coming out of SQL Server, you can also use
the REPLICATE keyword in your select statement to pad the int with
leading zeros:

http://sqlblogcasts.com/blogs/tonyro...05/29/765.aspx
Jan 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
2
by: Stian | last post by:
Hi, I'm sort of new to Python and I've just discovered the amazing formatting using the % operator. One thing I can't figure out is how to format numbers so that they get leading zeros, for example...
3
by: cody | last post by:
How can I format numbers with leading zeros? I believe I did this before some time ago but I cannot remember how I did it. I want the output be 100305 for 10:03:05. string fname =...
2
by: Ingo Nolden | last post by:
Hello, I want to format floats ( doubles ) into small strings of 7 characters. I tried to use std::ostream, but I don't get the desired behaviour. Especially in scientific notation I am not...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
5
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit...
2
by: chris | last post by:
Hi, I have a simple ms access application that allows you to scan barcodes in to a form which stores them in the database. The barcodes are 6 digits in length e.g. 555666 but my handheld...
4
by: Vlinder | last post by:
I'm completely new to PHP (jumped ship this week from MSAccess and ASP to MySql and PHP) and I'd appreciate some help with leading zeros in telephone numbers. I've got a field with type...
2
by: lawpoop | last post by:
Hello all -- I'm trying to format decimal numbers for a report. I would have to have the number with leading zeros to the tens' place, and trailing zeros to the thousandths' place. I'm using...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.