Monday 20 January 2014

Add zero before number if less than 10

To add zero in our string or integer if value is less than 10, can be achieved in several ways, like:

int testNumber = 9;

// 1: just stick a zero before it
string sFormatted1 = string.Format("0{0}", testNumber );
// 2: use a special format string to use at least two digits
string sFormatted12 = string.Format("{0:00}", testNumber );
// 3: pad the string with zeros to the left
string sFormatted3 = testNumber .PadLeft(2, '0');
// 4: manually concatenate with "0"
string sFormatted4 = "0" + testNumber ;

0 comments:

Post a Comment


                                                            
 
Design by Abhinav Ranjan Sinha