Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过c中的代码更改货币#_C# - Fatal编程技术网

C# 通过c中的代码更改货币#

C# 通过c中的代码更改货币#,c#,C#,我使用以下内容显示金额: String.Format(“{0:C}”,item.Amount) 这次展览花费9.99英镑 这没关系,但是如果我希望应用程序能够控制货币,并且能够在今天更改货币,该怎么办 9.99美元 如何通过代码更改货币格式 CultureInfo info = new CultureInfo (System.Threading.Thread.CurrentThread.CurrentCulture.LCID); info.NumberFormat.CurrencySymbol

我使用以下内容显示金额:

String.Format(“{0:C}”,item.Amount)

这次展览花费9.99英镑

这没关系,但是如果我希望应用程序能够控制货币,并且能够在今天更改货币,该怎么办

9.99美元

如何通过代码更改货币格式

CultureInfo info = new CultureInfo (System.Threading.Thread.CurrentThread.CurrentCulture.LCID);
info.NumberFormat.CurrencySymbol = "EUR";

System.Threading.Thread.CurrentThread.CurrentCulture = info;

Console.WriteLine (String.Format ("{0:C}", 45M));


在对
格式的调用中指定区域性:

    decimal value = 123.45M;
    CultureInfo us = CultureInfo.GetCultureInfo("en-US");
    string s = string.Format(us, "{0:C}", value);

货币符号由CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol定义。该属性为读/写属性,但如果尝试更改它,可能会出现异常,因为NumberFormatInfo.IsReadOnly将为true

或者,您可以显式使用特定的NumberFormatInfo设置数字格式:

NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
nfi.CurrencySymbol = "$";
String.Format(nfi, "{0:C}", item.Amount);

如果更改金额的显示货币,则也将更改其计量单位。£1 €1 $1. 您完全确定这里的业务需求吗?

对于在公司办公桌上运行的应用程序来说,以多种货币显示价值似乎是一个非常常见的要求……是的,我理解您的观点,此设置用于链接时区设置,用于整个应用程序,而不是单个案例。这与格式化日期和时间不同。即使您只需要为整个应用程序配置一种用户可配置的默认货币,也不能免除您将所有金额从一种货币转换为另一种货币的任务。此外,如果您以货币X存储值,并将其显示为货币Y,您不仅需要访问某种货币转换服务来显示新值,还必须向最终用户明确金额是一个估计值。没错,您不应该更改线程的当前区域性,特别是不应该简单地更改货币符号。使用CultureInfo.GetCultureInfo(“区域性字符串”)获取适当的区域性信息(以及所有格式设置)。不要忘记,您需要单独存储价格,或者允许(随时间变化)汇率——否则,我将以美元向英镑信用卡订购:)
NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
nfi.CurrencySymbol = "$";
String.Format(nfi, "{0:C}", item.Amount);