Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pad with leading zeros [duplicate]

Tags:

c#

How can i pad my integer variable with leading zeros. like i have an integer abc with value 20 but i want it to be '0000020'.

Code:

quarterlyReportDataCMMS.QRTrailerRecord.FileRecordCount = Convert.ToInt32(refEligibleClaimants.Count); 
like image 971
Ashutosh Avatar asked Aug 11 '10 14:08

Ashutosh


1 Answers

There's no such concept as an integer with padding. How many legs do you have - 2, 02 or 002? They're the same number. Indeed, even the "2" part isn't really part of the number, it's only relevant in the decimal representation.

If you need padding, that suggests you're talking about the textual representation of a number... i.e. a string.

You can achieve that using string formatting options, e.g.

string text = value.ToString("0000000"); 

or

string text = value.ToString("D7"); 
like image 108
Jon Skeet Avatar answered Oct 01 '22 11:10

Jon Skeet