Formatting In Decimal Number Using Windows Store Apps

Introduction

Today I am going to explain how to perform formatting in decimal numbers. We can do this by using the DecimalFormatter class of the Windows.globalization.NumberFormatting namespace which is applicable only to Windows Store apps. This class is used to format and parse decimal numbers. Their are various methods and properties that we use in this class.

In this article I am explaining some methods and properties that are used in this class to perform formatting. The namespace is added to the application so that we can use this DecimalFormatter class as:

using Windows.Globalization.NumberFormatting;

First of all to work with its methods and properties you need to create the instance of this class as:

DecimalFormatter df = new DecimalFormatter();

Now use the following steps to work with this class step by step.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". Select Windows Store inside the Visual C# from the left side and BlankPage from the center pane. Give a name for your application and then click "Ok".

Step 2

Add a namespace and create the instance of this DecimalFormatter class as I explained earlier.

Step 3


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

<Page

    x:Class="App35.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:App35"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid>

        <Grid.Background>

            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="#FFC6F5D3" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <Grid.RowDefinitions>

            <RowDefinition Height="100"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

            <RowDefinition Height="50"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="450"/>

            <ColumnDefinition Width="*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Decimal Number 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="Decimal Formatter:" Grid.Row="2" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

        <TextBlock Name="Decimal_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="Decimal Formatter (Grouped):" Grid.Row="4" Grid.Column="0" FontSize="20" Margin="10,10,0,0"/>

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

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

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

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

        <TextBlock Name="Decimal_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"/>

    </Grid>

</Page>

Step 4

Write the code in the MainPage.xaml.cs file as:
 

namespace App35

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            DecimalFormatter df = new DecimalFormatter();

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

            ulong fixedNumber = 500;

            Random_Number.Text = " " + randomNumber;

            Decimal_Formatter.Text = " " + df.Format(randomNumber);

            Fixed_Number.Text = "" + fixedNumber;

            df.IsGrouped = true;

            Decimal_Formatter_Grouped.Text = " " + df.Format(fixedNumber);

            df.FractionDigits = 0;

            Decimal_Formatter_NoFractonal.Text = "" + df.Format(fixedNumber);

            df.IsDecimalPointAlwaysDisplayed = true;

            Decimal_Formatter_alwaysdecimal.Text = "" + df.Format(fixedNumber);

        }

    }

}

In the preceding example I use the properties as:

IsGrouped : This property gets or sets whether the integer part of the number should be grouped or not.
FractionDigits  :  This gets or sets the minimum number of digits to display for the fraction part of the number.

IsDecimalPointAlwaysDisplayed  :  This gets or sets whether the decimal point of the number should always be displayed.

 

The method that I used in this example is the following.

Format : It will return a string representation of a Double number, if the argument in this is of Double type.


Step 5


Run the application; the output looks like:

Decimal-Formatter-output-screen-in-Windows-Store-apps.jpg

When I click on the button, the values are shown as:

Decimal-Formatter-In-Windows-Store-Apps.jpg

Summary

In this article I explained how to use the DecimalFormatter class which is applicable only to the Windows Store apps.

Up Next
    Ebook Download
    View all
    Learn
    View all