Tuesday 31 October 2017

Formatting of numbers by leading zeros in C#.

In this article we will discuss about How to Format of Numbers by leading Zeros in C#.

Format of leading zeros in the sense we can add any number of leading zeros to and existing number.

To add leading zeros we have a method called as PadLeft() which contains 2 parameter i.e totalWidth and paddingChar.

Example:-

Let me take an number which is of String type.

string strNumber="1234";

In this number i want to add leading 4 zeros and which is given below,

string strLeadingNumber=strNumber.PadLeft(8,'0');

Here in the above line my totalWidth of String should be 8 and it will add leading zeros.

Here the ouput will Show like this, 00001234

Let's say i have another requirement that if i don't have any value then what should it display?       Well it display all 8 zeros.

        This PadLeft() is present only in string and later on if you want to use this value as int then you can convert it from your own.

0 comments:

Post a Comment