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.
A
CultureInfocontains aNumberFormatInfoand this class describes (among other things) how to format currency for that particular culture.In particular you can use
CurrencyPositivePatternandCurrencyNegativePatternto determine if the currency symbol is placed before or after the amount and of courseCurrencySymbolto get the correct currency symbol. All this information is used by .NET when theCformat 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,CurrencyPositivePatternandCurrencyDecimalDigitsand thus is incomplete:Of course you could simply use: