473,427 Members | 1,760 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,427 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 4730
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
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...

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.