473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

CSharp String.Format, howto pad numbers with zeros

1
I stumbled upon this question about padding with zeros in csharp. Since it's locked and doesn't have the best answer, I decided to post this article..

http://bytes.com/topic/c-sharp/answe...-padding-zeros

This page lists the string formatting operators valid as ToString arguments. While it's not obvious, these are all also valid in String.Format()

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Therefore, if this is what you need:

Input number Need
1 00000001
121 00000121
10567 00010567

You can get it by doing:

String.Format("{0:D8}",1);
String.Format("{0:D8}",121);
String.Format("{0:D8}",10567);
Feb 24 '12 #1
2 4711
RhysW
70
Feel i should also point out this is useful for snipping off the ends of ridiculously long decimal numbers too if properly used, simple yet effective piece of code
Apr 12 '12 #2
PsychoCoder
465 Expert Mod 256MB
You could also use String.PadRight and String.PadLeft for this.
Apr 15 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Von Shean | last post by:
I use the following code to format the numbers to have commas. But in case when input is zero it returns an empty string....any ideas? String.Format("{0:#,###.##}",dblNum);
7
by: Roger Helliwell | last post by:
Hello Everyone, Has anyone found a quick reference for the many string formatting options for string.Format() ? I'm spending a ridiculous amount of time trying to write one line of code that...
2
by: Matthias S. | last post by:
Hi, I'm writing a logging utility and I need to wrap the string.Format method. I'd like to call the Trace method of MyLog like this: MyLog.Trace("Var1:{0};Var2:{1};", MyVar1, MyVar2); The...
3
by: Boz | last post by:
Hi, I am trying to use string.Format() to output the value of a double. double da = 100000000000.99994; double db = 100000000000.9994; double dc = 100000000000.994; double dd =...
6
by: A.M-SG | last post by:
Hi, I have the following code at the heavily demanded server side service: strResult=str1+"\\"+str2+"\\"+str3+"\\"; Would it be better (performance wise) that we change it to this: ...
5
by: RSH | last post by:
I have strings that are being converted that need to be eight characters long. The values are coming in as anywhere between 1 and 8 characters long. I need to pad the strings with zeros to make...
4
by: clintonG | last post by:
TimeSpan utcOffset = localZone.GetUtcOffset(DateTime.Now); MessageLabel.Text = utcOffset.ToString( ); // returns -06:00:00 Instead of -06:00:00 I want -06:00. Apparently the custom formatter...
2
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have the following: String.Format("{0}", myNumber); What is {0} called? The reason I ask is that I want to look up how present myNumber as 2 digits in the string. For example, if myNumber is...
3
by: hharry | last post by:
Hello All, I am using the following to format numbers precise to 2 decimal places with thousand separators: Double number = 100000.87; string MyString = String.Format("{0:0,0.00}", number);...
5
by: EricW | last post by:
Hi, I have the following line in my code: strConnect = String.Format("Data Source={0},{1};Network Library=DBMSSOCN;Initial Catalog={2};Integrated Security=False;Trusted...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.