How to add leading zeros to a number

I saw the question “How to add leading zeros to a number” on the ASP.NET forums countless times before. And often the answers provide solutions that work, but are overkill as well.

The two cleanest methods I know are String.Format and PadLeft.

   1:  Console.WriteLine("Using .ToString()");
   2:  Console.WriteLine(String.Format("{0:0000}", 16));
   3:   
   4:  Console.WriteLine("-------------------");
   5:   
   6:  Console.WriteLine("Using .PadLeft()");
   7:  Console.WriteLine(Convert.ToString(16).PadLeft(4, '0'));

You can see the result here.

//Using .ToString()
//0016
//-------------------
//Using .PadLeft()
//0016
Advertisements

How To Make Your Own Header File in Turbo C/C++

STEP 1: create new file (FILE > NEW) and write following syntax in it………….

#ifndef<space>__AMIT_H  //NAME OF YOUR HEADER FILE
#define<space>__AMIT_H
int factorial(int  num) //define your function directly
{
int i,f=1;
for(i=n;i>=1;i–)
f*=i;
return(f);
}
#endif
/* if you wnt to print somethi g in function you would require printf( ) so you can include stdio.h in function definition also
STEP 2: SAVE THIS FILE AS ANKIT.H  IN INCLUDE FOLDER OF TC  //YOU CAN USE ANY NAME //DEFINED ABOVE

#ifndef<space>__AMIT_H  //NAME OF YOUR HEADER FILE
 #define<space>__AMIT_H
#include<stdio.h>
………………………………….
…………………
*/Now you can call your Header file like other files.
#include<ankit.h>  //your header file name
#include<stdio.h>
#include<conio.h>
void main( )
{ int c;
clrscr( );
c=factorial(5);
printf(“factorial is %d”,c);  //change code accordingly in cpp
getch( );
}