Author : Guest
Total Views : 238
Posted On : Wed, Apr 4, 2018
Updated On :

Formatting Integer using String.Format in C#

In .Net we can format integer numbers in different ways. To format either we can use the instance method of integer class or we can use the static method called String.format.
The following examples show the uses of String.Format method to format the different integers in different ways.

Aligning an integer number to the left or right

In the following example, zero(0) is being appended to left or right based on the condition.

Console.WriteLine(String.Format("{0,5:0000}", 10));          // " 010"
Console.WriteLine(String.Format("{0,-5:0000}", 10));         // "0010 "

In the following example space(' ') is being appended to left or right based on the condition. If the number is negative then space will be appended to right else it will to the left.

Console.WriteLine(String.Format("{0,5}", 10));              // "   10"
Console.WriteLine(String.Format("{0,-5}", 10));             // "10   "
Console.WriteLine(String.Format("{0:0000}", 10));          // " 0010"
Console.WriteLine(String.Format("{0:0000}", -10));         // " -0010"

Some useful formatting

The .Net framework provides a different kind of formatting as well. Like you can apply special formatting for negative numbers and zeros.
This kind of formatting basically has three sections separated by a semicolon(;), the first is for the number, the second section is used to determine the negative number and last is used for zero.

Console.WriteLine(String.Format("{0:#;minus; #}", 10));      // "10"
Console.WriteLine(String.Format("{0:#;minus #}", -10));     // "minus 10"
Console.WriteLine(String.Format("{0:#;minus #;zero}", -10));  // "zero"

.Net Provides another way through which we can create our own custom formatting. Custom formatting can be used to display different set numbers like phone numbers, pin codes, emails, serial numbers etc.

Console.WriteLine(String.Format("{0:+### ###-###-###}", 334585748574)); // "+334 585 748 574"
Console.WriteLine(String.Format("{0:##-####-####}", 9856874526));       // "98-5687-4526"

Following is the example of all formatting described above.

Example

Try It
class IntegerFormatting
{
    static void Main(string[] args)
    {
        //Aligning an integer number to the left or right
        //In the following example zero(0) is being appended to left or right based on the condition.
        Console.WriteLine(String.Format("{0,5:0000}", 10));          // " 010"
        Console.WriteLine(String.Format("{0,-5:0000}", 10));         // "0010 "
 
 
        //In the following example space(' ') is being appended to left or right based on the condition.
        //If the number is negative then the space will be appended to right else it will to the left.
        Console.WriteLine(String.Format("{0,5}", 10));              // "   10"
        Console.WriteLine(String.Format("{0,-5}", 10));             // "10   "
        Console.WriteLine(String.Format("{0:0000}", 10));          // " 0010"
        Console.WriteLine(String.Format("{0:0000}", -10));         // " -0010"
 
        //Some useful formating
        Console.WriteLine(String.Format("{0:#;minus; #}", 10));      // "10"
        Console.WriteLine(String.Format("{0:#;minus #}", -10));     // "minus 10"
        Console.WriteLine(String.Format("{0:#;minus #;zero}", -10));  // "zero"
 
        Console.WriteLine(String.Format("{0:+### ###-###-###}", 334585748574)); // "+334 585 748 574"
        Console.WriteLine(String.Format("{0:##-####-####}", 9856874526));       // "98-5687-4526"
    }
}

Output

Output

0010
0010
10
10
0010
-0010
10
minus 10
minus 10
+334 585-748-574
98-5687-4526