Formatting In Percentages Using Windows Store Apps


Today I am going to explain the PercentFormatter class which resides in the Windows.Globalization.NumberFormatting namespace. This class is used to calculate the result and returns the percentage of that result or number. Or we can say that this class is used to format and parse the percentages. This class is only applicable to the Windows Store apps. Now to work with this class use the following steps.

Step 1

Open Visual Studio 2012 and click on "File" -> "New" -> "Project...", select Windows Store inside Visual C# from the left side pane and BlankPage from the center pane.

Step 2

Add the namespace:

using Windows.Globalization.NumberFormatting;

Step 3

Create the instance of this class as:

PercentFormatter pf = new PercentFormatter();

Step 4

Open the MainPage.xaml file and write the code as:









    <Grid Background="#FFF9CDC3">


            <RowDefinition Height="100"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>



            <ColumnDefinition Width="450"/>

            <ColumnDefinition Width="*"/>


        <TextBlock Text="Percent Formatting" FontSize="25" Grid.Row="0" Grid.Column="1" Margin="20,10,0,0"/>

        <TextBlock Text="Random Number:" Grid.Row="1" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Random_Number"  Grid.Row="1" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <TextBlock Text="Percent Formatter:" Grid.Row="2" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Percent_Formatter"  Grid.Row="2" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <TextBlock Text="Fixed Number:" Grid.Row="3" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Fixed_Number"  Grid.Row="3" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <TextBlock Text="Percent Formatter (Grouped):" Grid.Row="4" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Percent_Formatter_Grouped"  Grid.Row="4" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <TextBlock Text="Percent Formatter (No Fractional Digit):" Grid.Row="5" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Percent_Formatter_NoFractonal"  Grid.Row="5" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <TextBlock Text="Percent Formatter (Always with a Decimal Point):" Grid.Row="6" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Percent_Formatter_alwaysdecimal"  Grid.Row="6" Grid.Column="1" FontSize="20" Margin="34,10,347,10"/>

        <Button Content="Get Data" Grid.Row="7" Grid.Column="1" Height="50" Width="150" Click="Button_Click_1" Margin="20,10,0,-10"/>



Step 5

Write the following code in MainPage.xaml.cs:

namespace App35


    public sealed partial class MainPage : Page


        public MainPage()




        private void Button_Click_1(object sender, RoutedEventArgs e)


            PercentFormatter pf = new PercentFormatter();

            double randomNumber = new Random().NextDouble();

            ulong fixedNumber = 50;

            Random_Number.Text = " " + randomNumber;

            Percent_Formatter.Text = " " + pf.Format(randomNumber);

            Fixed_Number.Text = "" + fixedNumber;

            pf.IsGrouped = true;

            Percent_Formatter_Grouped.Text = " " + pf.Format(fixedNumber);

            pf.FractionDigits = 0;

            Percent_Formatter_NoFractonal.Text = "" + pf.Format(fixedNumber);

            pf.IsDecimalPointAlwaysDisplayed = true;

            Percent_Formatter_alwaysdecimal.Text = "" + pf.Format(fixedNumber);




Step 6

Now Run the application. The output of this application looks like:



In this article I explained how to use the PercentFormatter class.