Use of string.Format method in C# with an example?

About string.Format

String.Format creates string from a pattern and values. Returns the formatted string according to "pattern format" defined in the "format pattern control" parameter. So we can format a string into the following format as shown below.

Following Format Characters like

1) Decimal separator (.)
2) Percent character (%)
3) Comma (,)
4) Pound character (Digit Placeholder) (#)
5) Currency (c)
6) Number (N)
7) Scientific (e)
8) Thousand Separator (n)
9) Zero Placeholder (00)

Each above format character has it own "pattern" which you have defined in the "format pattern control" then only it will show you the proper string format output.

Demonstration of Format characters using pattern control

Note : I have demonstrated each and every example in Console Application using C-sharp code

1) Decimal separator (.)

Decimal separator denoted by (.).

For one decimal places use pattern "0:0.0" or for two "0.00" and so on. If an input number has less decimal place, the rest digits on the right will be zeroes (0). If it has more decimal places, the number will be rounded .Below snippet shows an example with one decimal place.

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(String.Format("Output Format For Decimal Separator: {0:0.0}", input));

//Output 12345.1
}

2) Percent Character (%)

Percent Character denoted by (%).Multiplies by 100, adds % sign. if an input number doesn't have any decimal then it adds (00%) to the end of input numbers.

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(String.Format("Output Format For Percentage: {0:0%}", input));
//Output 1234512%
}

3) Comma (,)

Comma Character denoted by (,).It is also known as Thousand separator. It separates thousand number by adding (,) to the number.

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(String.Format("Output Format For Comma Separator: {0:0,0}", input));

//Output 12,345
}

4) Pound Character (#)

Pound Character denoted by (#).It is also known as Digit Placeholder. It adds the round braces to the number from both sides and rounds the decimal number (if number with decimal value).

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(String.Format("Output Format For Digit Placeholder: {0:(#).##}", input));

//Output (12345).12
}

5) Currency (c)

Currency Character denoted by (c).It outputs the format character in a money format with a USD($) dollar symbol. Usage of currency format as shown below

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(string.Format("Output Format For Currency: {0:c}", input));

//Output $12,345.00
}

6) Number(N)

Number Character denoted by (N).It outputs the format character in a number format. Usage of number format as shown below

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(string.Format("Output Format For Number: {0:N}", input));

//Output 12,345.00
}

7) Scientific (e)

Scientific Character denoted by (e).Usage of scientific format as shown below

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(string.Format("Output Format For Scientific: {0:e}", input));

//Output 1.234500e+004
}

8) Thousand Separator (n)

Thousand Separator Character denoted by (n).It separates the following large number with a comma after thousand. Usage of thousand separator format as shown below

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(string.Format("Output Format For Thousand Separator: {0:n}", input));

//Output 12,345.00
}

9) Zero Placeholder (00)

Zero Placeholder Character denoted by (00). Usage of zero placeholder format as shown below

 
static void Main(string[] args)
{
 Console.WriteLine("Please Enter Any Number Eg: 12345.12");
 double input = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine(string.Format("Output Format For Zero Placeholder: {0:00.0000}", input));

//Output 12345.0000
}

So friends this all about different format characters with their pattern using in string.Format method. If you like this article kindly share it with your friends.

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Comments

64x64
By SINDHI PANKAJ GHANSHYAMBHAI on 2013-10-01
very good, thank you

Add a Comment