ARTICLE

Currency Formatter Class In Windows Store Apps

Posted by Richa Garg Articles | Windows Store Apps September 28, 2012
In this article I will explain how to format a number as a currency.
Reader Level:

Introduction

Today I am going to explain how to Format and Parse a number as a currency. This can be done using the Currency Formatter class which resides in the Windows.Globalization.NumberFormatting namespace. This class is only applicable in Windows Store apps. Now to see how this works just follow the instuctions here.

Step 1

Open Visual Studio 2012 and create a BlankPage and give the name of your application as "CurrencyFormatter" then click ok.

Step 2

Add the namespace in your application as:

using Windows.Globalization.NumberFormatting;

Step 3

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

<Page

    x:Class="CurrencyFormatter.MainPage"

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

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

    xmlns:local="using:CurrencyFormatter"

    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="#FF1DEC39" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <TextBlock Text="Currency Formatter Example" FontSize="25" FontFamily="Script MT Bold" HorizontalAlignment="Left" Height="50" VerticalAlignment="Top" Margin="10,10,0,0" Padding="10" Foreground="#FF4841E8" FontWeight="Bold"/>

        <TextBlock Text="This application shows the use of Currency Formatter class which is used to format a number as a currency." FontSize="20" FontFamily="Times New Roman" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,77,0,0"/>

        <Button Content="Display" Height="50" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,136,0,0" Padding="10" Click="Button_Click_1"/>

        <TextBlock Name="output" Margin="50,262,0,-94" Height="600" Width="850" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="20"/>

    </Grid>

</Page>

Step 4

In the MainPage.xaml.cs file, write the code as:
 

namespace CurrencyFormatter

{   

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();           

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            string currency = GlobalizationPreferences.Currencies[0];

            ulong wholeNumber = 12345;

            double fractionalNumber = 12345.67;

            StringBuilder results = new StringBuilder();

            Windows.Globalization.NumberFormatting.CurrencyFormatter cf = new Windows.Globalization.NumberFormatting.CurrencyFormatter(currency);

            //Create the instance of the Currency to convert in USD format

            Windows.Globalization.NumberFormatting.CurrencyFormatter usdCf = new Windows.Globalization.NumberFormatting.CurrencyFormatter("USD");

            //Currency converted into EURO format

            Windows.Globalization.NumberFormatting.CurrencyFormatter eurFRCf = new Windows.Globalization.NumberFormatting.CurrencyFormatter("EUR", new[] { "fr-FR" }, "FR");

            Windows.Globalization.NumberFormatting.CurrencyFormatter eurIECf = new Windows.Globalization.NumberFormatting.CurrencyFormatter("EUR", new[] { "gd-IE" }, "IE");          

            results.AppendLine("Fixed number (" + fractionalNumber + ")");

            results.AppendLine("Formatted with user's default currency: " + cf.Format(fractionalNumber));

            results.AppendLine("Formatted US Dollar: " + usdCf.Format(fractionalNumber));

            results.AppendLine("Formatted Euro (fr-FR defaults): " + eurFRCf.Format(fractionalNumber));

            results.AppendLine("Formatted Euro (gd-IE defaults): " + eurIECf.Format(fractionalNumber));

            results.AppendLine();

            // Format currency with fraction digits always included.

            usdCf.FractionDigits = 2;

            results.AppendLine("Formatted US Dollar (with fractional digits): " + usdCf.Format(wholeNumber));

            // Format currenccy with grouping.

            usdCf.IsGrouped = true;

            results.AppendLine("Formatted US Dollar (with grouping separators): " + usdCf.Format(fractionalNumber));

            output.Text = results.ToString();

        }

    }

}

In this application the number is formatted into the USD format of currency, the Euro format etc.; I also perform the grouping of the number in this type of formatting.

Step 5

Now run the application, and when you click the button the output is displayed as:

Currency-Formatter-In-Windows-Store-apps.jpg

Summary

In this article I explained how to perform formatting and parsing of a number as a currency.

COMMENT USING