Home » c# » Pad with leading zeros [duplicate]

Pad with leading zeros [duplicate]

Posted by: admin November 30, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

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

myString.PadLeft(myString.Length + 5, '0');

Questions:
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");

Questions:
Answers:

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")

Questions:
Answers:

You can use

String.PadLeft()
String.Format()

Questions:
Answers:

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!