469,352 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 27528
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Krakatioison | last post: by
2 posts views Thread by Stian | last post: by
2 posts views Thread by Ingo Nolden | last post: by
5 posts views Thread by Bilgehan.Balban | last post: by
2 posts views Thread by lawpoop | 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
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.