问题
In C# the code Console.WriteLine("{0:c}", 998);
gives the output $998
in default "US-Language" settings. But if I want to dynamically change my currency symbol to Pound, Sterling, Rupee or any currency symbol depending upon user preference, is there any way around to do this. Say, I call a method:
public void PrintInRightCurrencyFormat(decimal value, ICustomFormatter format)
{
Console.WriteLine( ... ... ... );
}
And this method will print the value in required format.
One more thing is that is there any way to insert a custom currency symbol. My point is that if a currency comes with a new symbol(Like India did with its Rupee symbol), how to enable that immediately in code.
Thank you all in advance.
回答1:
You could use a culture:
Console.WriteLine(string.Format(new CultureInfo("en-GB"), "{0:c}", value));
or simply set the current thread culture to some user preference and then print the value:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Console.WriteLine("{0:c}", value);
来源:https://stackoverflow.com/questions/5840106/currency-formatting-with-dynamic-currency-symbol