Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific culture currency pattern

Tags:

How do i get the currency pattern for a specific culture?

For Example:

Instead of using:

string.Format("{0:c}", 345.10) 

I want to use this:

string.Format("#.##0,00 €;-#.##0,00 €", 345.10); 

But how do i get the pattern string (like "#.##0,00 €;-#.##0,00 €") for each culture my application needs?

I cant use the "{0:c}" pattern because if the user switches the language the currency should be the same.

like image 823
naishx Avatar asked Aug 03 '11 09:08

naishx


People also ask

How to apply currency format in c#?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.

What is a currency formatter?

CurrencyFormatter. js allows you to format numbers as currencies. It contains 155 currency definitions and 715 locale definitions out of the box. It handles unusually formatted currencies, such as the INR.


2 Answers

A CultureInfo contains a NumberFormatInfo and this class describes (among other things) how to format currency for that particular culture.

In particular you can use CurrencyPositivePattern and CurrencyNegativePattern to determine if the currency symbol is placed before or after the amount and of course CurrencySymbol to get the correct currency symbol. All this information is used by .NET when the C format specifier is used.

You can read more about the NumberFormatInfo class on MSDN.

The code below demonstrates some of the steps required to format currency properly. It only uses CurrencySymbol, CurrencyPositivePattern and CurrencyDecimalDigits and thus is incomplete:

var amount = 123.45M; var cultureInfo = CultureInfo.GetCultureInfo("da-DK");  var numberFormat = cultureInfo.NumberFormat; String formattedAmount = null; if (amount >= Decimal.Zero) {   String pattern = null;   switch (numberFormat.CurrencyPositivePattern) {     case 0:       pattern = "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}";       break;     case 1:       pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}";       break;     case 2:       pattern = "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}";       break;     case 3:       pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}";       break;   }   formattedAmount = String.Format(cultureInfo, pattern, numberFormat.CurrencySymbol, amount); } else {   // ... }  Console.WriteLine(formattedAmount); 

Of course you could simply use:

var amount = 123.45M; var cultureInfo = CultureInfo.GetCultureInfo("da-DK"); var formattedAmount = String.Format(cultureInfo, "{0:C}", amount); Console.WriteLine(formattedAmount); 
like image 103
Martin Liversage Avatar answered Oct 27 '22 16:10

Martin Liversage


I think what you're asking is how to change the currency symbol but keep the culture-specific formatting. You can do this by getting a copy of the current NumberFormatInfo and modifying the CurrencySymbol property:

Thread.CurrentThread.CurrentCulture = new CultureInfo("de"); // pretend we are german  var nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone(); nfi.CurrencySymbol = "$$$"; Console.WriteLine(string.Format(nfi,"{0:c}",345.10)); 

This will output:

345,10 $$$ 

Without changing the CurrentCulture it outputs (for me):

$$$345.10 
like image 30
porges Avatar answered Oct 27 '22 16:10

porges