The integer data type is a more than sufficient way to represent whole numbers. But what happens when you need all your numbers to display as 16 digits, with the number left padded with zeros? The integer data type in C# is unable to handle displaying a whole number left padded with zeros.
PadLeft(int totalWidth, char paddingChar)
The key to this is that you will need to first convert the integer to a string, and then apply the PadLeft function. Below is our example of converting an three digit integer into a sixteen digit string consisting of only numbers.
int myNumber = 234; string myStringNumber = myNumber.ToString().PadLeft(16, '0');
Note that this quick easy method will that trouble handling negative numbers. But with a couple of simple checks, it should be relatively easy to handle that case. Also note that this PadLeft is not restricted to numbers, but can also be used to insert additional letters or symbols in regular string literals.
RSS Feed
Posted in
Tags: 
