94

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);
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
Ashutosh
  • 5,614
  • 13
  • 52
  • 84

5 Answers5

157

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");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I tried that but it says Cannot implicitly convert type string to int – Ashutosh Aug 11 '10 at 15:03
  • 4
    @Ashutosh: No, the point is you *want* it as a string. There's no such concept as a padded int - an int is *just a number*. – Jon Skeet Aug 11 '10 at 15:07
  • 2
    @Jon but your code is trying to implicitly convert a string to an int... (`int text = value.ToString("000")`) – Steven Oxley Aug 11 '10 at 15:18
  • 3
    @Steven: Doh - will fix that. I'm a fool! Apologies to @Ashutosh and anyone else who tried this. I'll hang my head in shame :( – Jon Skeet Aug 11 '10 at 15:21
  • @Jon: No problem John..I fixed it somehow. I am a novice in this field who fortunately got an internship, so i am learning all these things to fulfill the expectations. thanks – Ashutosh Aug 11 '10 at 15:40
  • @TomGullen plus it is a code **name** it's entirely the fault of the naming convention :P – Eonasdan Nov 12 '12 at 22:10
148

You can do this with a string datatype. Use the PadLeft method:

var myString = "1";
myString = myString.PadLeft(myString.Length + 5, '0');

000001

Demodave
  • 6,242
  • 6
  • 43
  • 58
Matt B
  • 8,315
  • 2
  • 44
  • 65
  • 42
    Alternatively, if you wanted fixed number of places where 5 is the total number of placed replaced with 0: myString.PadLeft(5, '0') – Ben Sep 25 '14 at 21:03
  • Although this answer is useful, it's not the way to do it unless you're starting out with a string. – Chris Peacock Jan 24 '20 at 17:44
20

An integer value is a mathematical representation of a number and is ignorant of leading zeroes.

You can get a string with leading zeroes like this:

someNumber.ToString("00000000")
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
15

The concept of leading zero is meaningless for an int, which is what you have. It is only meaningful, when printed out or otherwise rendered as a string.

Console.WriteLine("{0:0000000}", FileRecordCount);

Forgot to end the double quotes!

Scott
  • 65
  • 1
  • 1
  • 11
James Curran
  • 101,701
  • 37
  • 181
  • 258
15

You can use

String.PadLeft()
String.Format()

quantumSoup
  • 27,197
  • 9
  • 43
  • 57