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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1017139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:38:05  来源:igfitidea点击:

Changing the Currency via code in c#

c#

提问by Coppermill

I am using the following to display an amount:

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

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

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

This display £9.99

此显示£9.99

which is okay, but what if I want the application to be able to control the currency and to be able to change the currency to day

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

$9.99

9.99 美元

How do I change the currency format via code

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

采纳答案by Marc Gravell

Specify the culture in the call to Format:

在调用中指定区域性Format

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

回答by Frederik Gheysels

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));

or

或者

NumberFormatInfo info = new NumberFormatInfo ();
info.CurrencySymbol = "EUR";

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

回答by Thomas Levesque

The currency symbol is defined by CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol. The property is read/write but you will probably get an exception if you try to change it, because NumberFormatInfo.IsReadOnly will be true...

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

Alternatively, you could format the number by explicitly using a specific NumberFormatInfo :

或者,您可以通过显式使用特定的 NumberFormatInfo 来格式化数字:

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

回答by Christian Hayter

If you change the displayed currency of an amount, then you are changing its unit of measurement too. £1 <> 1 <> $1. Are you absolutely sureof the business requirement here?

如果您更改金额的显示货币,那么您也在更改其计量单位。£1 <> 1 <> $1。您绝对确定这里的业务需求吗?