Currency Formatting With Dynamic Currency Symbol

老子叫甜甜 提交于 2021-02-05 08:32:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!