Percent ("P") Format Specifier In C#

Introduction

The Percent ("P") Format Specifier in C# is used to convert any number to the string type by multiplying the number by 100. We can also say that the Percent Format can multiply the number by 100 and convert it to the string that denotes percentage. The property is followed by the NumberFormatInfo class. It is one of the types of the Standard Numeric Format String. To convert any number into a percent write the following code in the console application as:

namespace ConsoleApplication7

{

    class Program

    {

        static void Main(string[] args)

        {

            double number = 0.1234;

            Console.WriteLine("The percent format is as : " + number.ToString("P", CultureInfo.InvariantCulture));

            double no = 0.05;

            Console.WriteLine("The percent format is as : " + no.ToString("P", CultureInfo.InvariantCulture));

        }

    }

}


Output

Display-percent-formatting-in-C-SHARP.jpg

Properties of Percent Format Specifier

PercentDecimalDigit :
 By using this property we can specify the digits after the after point; in other words the number of digits after the decimal point will be decreased or increased using this property. This property is defined under the NumberFormatInfo class that we apply in this Format as:
 

namespace ConsoleApplication7

{

    class Program

    {

        static void Main(string[] args)

        {

            // Gets a NumberFormatInfo associated with the en-US culture.

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            // Displays a value with the default number of decimal digits (2).

            Double myInt = 0.1234;

            Console.WriteLine("Percent Format : " + myInt.ToString("P", nfi));

            // Displays the same value with four decimal digits.

            nfi.PercentDecimalDigits = 4;

            Console.WriteLine("Percent Format with 4 Decimal digits : " + myInt.ToString("P", nfi));

            // Displays the same value with eight decimal digits.

            nfi.PercentDecimalDigits = 8;

            Console.WriteLine("Percent Format with 8 Decimal digits : " + myInt.ToString("P", nfi));

        }

    }

}

Output

Decimal-percent-formatting-In-C-sharp.jpg

PercentDecimalSeparator :
 By using this property we can separate the number with the number after the decimal point. This property is also assessed by the NumberFormatInfo class as:

 

namespace ConsoleApplication7

{

    class Program

    {

        static void Main(string[] args)

        {

            // Gets a NumberFormatInfo associated with the en-US culture.

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            // Displays a value with the default number of decimal digits (2).

            Double myInt = 0.1234;

            Console.WriteLine("Percent Format : " + myInt.ToString("P", nfi));

            // Displays the same value with Separation.

            nfi.PercentDecimalSeparator = " ";

            Console.WriteLine("Percent Format with Separation : " + myInt.ToString("P", nfi));

            // Displays the same value with Seperation by any symbol

            nfi.PercentDecimalSeparator = "@";

            Console.WriteLine("Percent Format with Separation by @ sign : " + myInt.ToString("P", nfi));

        }

    }

}

Output

Percent-decimal-separator-in-c-sharp.jpg

PercentGroupSeparator : By using this property we can separate the grouping; in other words if the number has a comma " ," then the comma will be removed and there will become a separation between numbers by a space as:
 

namespace ConsoleApplication7

{

    class Program

    {

        static void Main(string[] args)

        {

            // Gets a NumberFormatInfo associated with the en-US culture.

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;          

            Double myInt = 1234.5678;

            Console.WriteLine(myInt.ToString("P", nfi));

            // Displays the same value with a blank as the separator.

            nfi.PercentGroupSeparator = " ";

            Console.WriteLine(myInt.ToString("P", nfi));

        }

    }

}

Output

Percent-group-separator-in-percent-format-specifier-using-c-sharp.jpg

PercentGroupSizes :
By using this property we can group the numbers according to the sizes in the number. In the following example I describe the sizes in the array in which the last index of the array will become the number of digits of the number at the right hand side, and so on as:
 

namespace ConsoleApplication7

{

    class Program

    {

        static void Main(string[] args)

        {

            // Gets a NumberFormatInfo associated with the en-US culture.

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            // Displays a value with the default separator (".").

            Double myInt = 123456789012345.6789;

            Console.WriteLine("The Number is as : " + myInt.ToString("P", nfi));

            // Displays the same value with different groupings.

            int[] mySizes1 = { 2, 3, 4 };

            int[] mySizes2 = { 2, 3, 0 };

            nfi.PercentGroupSizes = mySizes1;

            Console.WriteLine("After apply the size 1 : " + myInt.ToString("P", nfi));

            nfi.PercentGroupSizes = mySizes2;

            Console.WriteLine("After apply the size 2 : " + myInt.ToString("P", nfi));

        }

    }

}

Output

Percent-group-sizes-in-percent-format-specifier-using-c-sharp.jpg

Summary

In this article I explained how to perform the formatting in percent using C#.

Up Next
    Ebook Download
    View all
    Learn
    View all