472,142 Members | 1,007 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 27599
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
reply views Thread by leo001 | last post: by

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.