# Converting a Number in Currency Format For Different Cultures

In this article we will learn how to display a number in its currency format with respect to a country. When we want to display a number in its respective country's currency format, we format the string in the currency format and get the currency symbol of a specific country using the "CultureInfo" class available in .Net. To use the CultureInfo class, we need to include the "System.Globalization" Namespace in our program.

The "C" format specifier converts a number to a string that represents a currency amount. The precision specifier indicates the desired number of decimal places in the result string. If the precision specifier is omitted then the default precision is used (the default value is 2).

If the value to be formatted has more than the specified or the default number of decimal places then the fractional value is rounded in the result string. If the value to the right of the number of the specified decimal places is 5 or greater then the last digit in the result string is rounded away from zero.

Here is code showing how to convert a number to its corresponding currency format for a respective country:

using System;

using System.Globalization;

namespace CurrencyFormatter

{

class Program

{

static void Main(string[] args)

{

double value = 5623345.6789;

//For Current Culture

Console.WriteLine("\n--------- Displaying Currency in Current Culture ---------------\n");

// By default, single letter C displays currency upto two decimal digits

Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

// C2 displays currency upto two digits

Console.WriteLine(value.ToString("C2", CultureInfo.CurrentCulture));

// C3 displays currency upto three digits

Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

// C4 displays currency upto four digits

Console.WriteLine(value.ToString("C4", CultureInfo.CurrentCulture));

// C5 displays currency upto five digits

Console.WriteLine(value.ToString("C5", CultureInfo.CurrentCulture));

//For Japan

Console.WriteLine("\n--------- Dispalying Currency for Japan ---------------\n");

Console.WriteLine(value.ToString("C", CultureInfo.CreateSpecificCulture("ja-JP")));

//For Denmark

Console.WriteLine("\n--------- Dispalying Currency for Denmark ---------------\n");

Console.WriteLine(value.ToString("C",

CultureInfo.CreateSpecificCulture("da-DK")));

//For India

Console.WriteLine("\n--------- Dispalying Currency for India ---------------\n");

Console.WriteLine(value.ToString("C",

CultureInfo.CreateSpecificCulture("en-IN")));

}

}

}

The result string is affected by the formatting information of the current NumberFormatInfo object. As per MSDN, the following table lists the NumberFormatInfo properties that control the formatting of the returned string.

 NumberFormatInfo property Description CurrencyPositivePattern Defines the placement of the currency symbol for positive values. CurrencyNegativePattern Defines the placement of the currency symbol for negative values, and specifies whether the negative sign is represented by parentheses or the NegativeSign property. NegativeSign Defines the negative sign used if CurrencyNegativePattern indicates that parentheses are not used. CurrencySymbol Defines the currency symbol. CurrencyDecimalDigits Defines the default number of decimal digits in a currency value. This value can be overridden by using the precision specifier. CurrencyDecimalSeparator Defines the string that separates integral and decimal digits. CurrencyGroupSeparator Defines the string that separates groups of integral numbers. CurrencyGroupSizes Defines the number of integer digits that appear in a group.

Now let us see some of the important properties of the "NumberFormatInfo" object in the following sections.

How to group the numbers using NumberFormatInfo.CurrencyGroupSizes property

It is used to get or set the number of digits in each group to the left of the decimal in currency values. To set grouping in a number, we assign this property to an integer array having a required grouping.

For example, if the array contains {2, 4, 3} then the digits are grouped similar to \$55, 555, 555, 554, 4443, 33.00". If the array contains {2, 4, 0} then the digits are grouped similar to \$55555555554, 4443, 33.00". That means that the grouping of digits starts from the left corresponding to array elements. If the digits in a number are greater than the sum of the array elements then the remaining digits in the number are grouped (from left to right) as per the value of the last element in the array, provided that the element is non-zero. If the last element in the array is Zero then grouping won't take place for the remaining digits.

The following code demonstrates how to set digits grouping in a number:

using System;

using System.Globalization;

class NumberFormatInfoSample

{

public static void Main()

{

// Gets a NumberFormatInfo associated with the USA culture.

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

// Displays a value with the default separator

long myInt = 55555555554444333;

Console.WriteLine("\nDisplays a value with the default separator ");

Console.WriteLine(myInt.ToString("C", myNumberFormatInfo));

// Displays the same value with different groupings.

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

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

myNumberFormatInfo.CurrencyGroupSizes = mySizes1;

Console.WriteLine("\nDisplays the same value with different group {2,4,3}  ");

Console.WriteLine(myInt.ToString("C", myNumberFormatInfo));

myNumberFormatInfo.CurrencyGroupSizes = mySizes2;

Console.WriteLine("\nDisplays the same value with different group {2,4,0}  ");

Console.WriteLine(myInt.ToString("C", myNumberFormatInfo));

}

}

It produces output
as in the following:

The following code shows how to set a group separator symbol using the NumberFormatInfo.CurrencyGroupSeparator property:

using System;

using System.Globalization;

class NumberFormatInfoSample

{

public static void Main()

{

// Gets a NumberFormatInfo associated with the USA culture.

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

Int64 myInt = 5674433277865454;

// Displays a value with following grouping.

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

myNumberFormatInfo.CurrencyGroupSizes = mySizes1;

Console.WriteLine("\nDisplays a value with default seperator symbol (,) : ");

Console.WriteLine(myInt.ToString("C", myNumberFormatInfo));

myNumberFormatInfo.CurrencyGroupSeparator = "_";

Console.WriteLine("\nDisplays the same value with seperator symbol (_) : ");

Console.WriteLine(myInt.ToString("C", myNumberFormatInfo));