C# 将整数转换为货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8913840/
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-09 05:08:48  来源:igfitidea点击:

Convert Integer to Currency

c#string-formattingcurrency

提问by Lee Treveil

We have a database that stores numbers(money) in base currency, so £21.30 would be stored as 2130 in the database. How would I go about converting that number to a correctly formatted currency for all cultures?

我们有一个以基础货币存储数字(货币)的数据库,因此 £21.30 将在数据库中存储为 2130。我将如何将该数字转换为适用于所有文化的正确格式的货币?

2130 -> £21.30
2130 -> $21.30
etc

2130 -> £21.30
2130 -> 21.30 美元

回答by Darin Dimitrov

int valueFromDb = 2130;
decimal result = valueFromDb / 100m;
string formatted = result.ToString("c");

Could be quickly extension methodified:

可以快速扩展方法化:

public static class CurrencyExtensions
{
    public static string AsCurrency(this int value)
    {
        return value.AsCurrency(CultureInfo.CurrentCulture);
    }

    public static string AsCurrency(this int value, CultureInfo culture)
    {
        decimal result = value / 100m;
        return result.ToString("c", culture);
    }
}

so that now you could be DRYier:

所以现在你可以更干燥:

int valueFromDb = 2130;
string formatted = valueFromDb.AsCurrency();

回答by Dennis Traub

decimal value = ((decimal)(myNumber / 100.0));
var result = String.Format("{0:c}", value);

回答by Blorgbeard is out

Here's how to do it in c# with string.format:

以下是如何在 c# 中使用 string.format 执行此操作:

decimal amount = 2130 / 100M;
string output = string.Format("{0:c}", amount);

That gets you the format for the current culture. More information on MSDN

这为您提供了当前文化的格式。有关 MSDN 的更多信息

回答by MyKuLLSKI

Try the following:

请尝试以下操作:

 decimal amount = 2130 / 100m;
 string output = value.ToString("C", CultureInfo.CurrentCulture);

回答by Jon Hanna

string asLocalCurrency(int intMoney)
{
  return (num / 100m).ToString("c"); //e.g. 12345 becomes 123.45 for me
}
string asSomeLocalCurrency(int intMoney, CultureInfo cInfo)
{
  return (num / 100m).ToString("c", cInfo); // e.g. 12345 with InvariantCulture is ¤123.45
}

However. This means that £123.45 becomes $123.45 or 123.45, which is clearly not correct.

然而。这意味着 £123.45 变成了 $123.45 或 123.45,这显然是不正确的。

Then there's the problem that Peso/Dollar sign $ can be used to represent NIO, AUD, CAD, TOP, USD, HKD, and a bunch more. It's not the only symbol that sees use in more than one places. If you're recording old payments even things like £ being used for IEP and ITL (no longer in use) can become an issue.

然后是比索/美元符号 $ 可用于表示 NIO、AUD、CAD、TOP、USD、HKD 等等的问题。它不是在多个地方使用的唯一符号。如果您正在记录旧付款,即使像 £ 用于 IEP 和 ITL(不再使用)之类的东西也可能成为问题。

So can the fact that you're storing the values as whole numbers of percentage-of-main-unit, which doesn't apply to all currencies.

您将这些值存储为主要单位百分比的整数这一事实也是如此,这并不适用于所有货币。

A monetary value is only meaningful with an implied or explicit currency. Here you're taking one that either has none, or you're replacing an implied with an explicit, or you're depending upon a very large piece of overriding logic to keep things correct.

货币价值仅对隐含或明确的货币有意义。在这里,您要么没有,要么用显式替换隐式,要么依赖于一个非常大的覆盖逻辑来保持正确。

The last of these can work, but is fraught. The other two are just wrong.

这些中的最后一个可以工作,但令人担忧。其他两个都错了。

Finally, culture at best tells you what currency people are most familiar with, but not what they'll always use. I often use kroner and ?re, but I use Hiberno-English when using them all the same.

最后,文化充其量只能告诉你人们最熟悉什么货币,而不是他们会一直使用什么货币。我经常使用 kroner 和 ?re,但在使用它们时我使用 Hiberno-English。

I'd recommend storing ISO 4217 codes along with the currencies. It's a good idea to have it somewhere on a final invoice too. The symbol is good for prettiness, the code is good for having things precise.

我建议将 ISO 4217 代码与货币一起存储。在最终发票上的某个地方也有一个好主意。符号有利于美观,代码有利于精确。

Ignore the user's culture, except perhaps as a best-guess at a default choice. (e.g. if they're American then they probablywant to use USD, but may not want to). There aren't that many currencies in the world, so here's a list of all bar a few specialised International-use cases:

忽略用户的文化,除非是对默认选择的最佳猜测。(例如,如果他们是美国人,那么他们可能想使用美元,但可能不想)。世界上没有那么多货币,所以这里列出了一些专门的国际用例:

AED  United Arab Emirates Dirham  ?.?
AFN  Afghan Afghani  ?
ALL  Albanian Lek  L
AMD  Armenian Dram  ??
ANG  Netherlands Antillean Guilder  ?
AOA  Angolan Kwanza  Kz
ARS  Argentine Peso  $
AUD  Australian Dollar  $
AWG  Aruban Florin  ?
AZN  Azerbaijani Manat  man.
BAM  Bosnia and Herzegovina Convertible Mark  KM
BBD  Barbados Dollar  $
BDT  Bangladeshi Taka  ?
BGN  Bulgarian Lev  лв
BHD  Bahraini Dinar  .?.?
BIF  Burundian Franc  FBu
BMD  Bermudian Dollar  $
BND  Brunei Dollar  $
BOB  Boliviano  Bs.
BRL  Brazilian Real  R$
BSD  Bahamian Dollar  $
BTN  Bhutanese Ngultrum  Nu.
BWP  Botswana Pula  P
BYR  Belarusian Ruble  Br
BZD  Belize Dollar  $
CAD  Canadian Dollar  $
CDF  Congolese Franc  FC
CHF  Swiss Franc  Fr.
CLP  Chilean Peso  $
CNY  Chinese Yuan  ¥
COP  Colombian Peso  $
CRC  Costa Rican Colon  ?
CUC  Cuban convertible Peso  $
CUP  Cuban Peso  $
CVE  Cape Verde Escudo  $
CZK  Czech Koruna  K?
DJF  Djiboutian Franc  Fdj
DKK  Danish Krone  kr
DOP  Dominican Peso  $
DZD  Algerian Dinar  ?.?
EEK  Estonian Kroon  kr
EGP  Egyptian Pound  ?.?
ERN  Eritrean Nakfa  Nfk
ETB  Ethiopian Birr  Br
EUR  Euro  
FJD  Fiji Dollar  $
FKP  Falkland Islands Pound  £
GBP  Pound Sterling (British Pound)  £
GEL  Georgian Lari  lari
GHS  Ghanaian Cedi  ?
GIP  Gibraltar Pound  £
GMD  Gambian Dalasi  D
GNF  Guinean Franc  FG
GTQ  Guatemalan Quetzal  Q
GYD  Guyanese Dollar  $
HKD  Hong Kong Dollar  $
HNL  Honduran Lempira  L
HRK  Croatian Kuna  kn
HTG  Haitian Gourde  G
HUF  Hungarian Forint  Ft
IDR  Indonesian Rupiah  Rp
ILS  Israeli New Sheqel  ?
INR  Indian Rupee  Rs
IQD  Iraqi Dinar  ?.?
IRR  Iranian Rial  ?
ISK  Icelandic Króna  kr
JMD  Jamaican Dollar  $
JOD  Jordanian Dinar  JD
JPY  Japanese Yen  ¥
KES  Kenyan Shilling  Ksh
KGS  Kyrgyzstani Som  som
KHR  Cambodian Riel  ?
KMF  Comoro Franc  CF
KPW  North Korean Won  ?
KRW  South Korean Won  ?
KWD  Kuwaiti Dinar  ?.?
KYD  Cayman Islands Dollar  $
KZT  Kazakhstani Tenge  ?
LAK  Lao Kip  ?
LBP  Lebanese Pound  ?.?
LKR  Sri Lanka Rupee  Rs
LRD  Liberian Dollar  $
LSL  Lesotho Loti  M
LTL  Lithuanian Litas  Lt
LVL  Latvian Lats  Ls
LYD  Libyan Dinar  ?.?
MAD  Moroccan Dirham  ?.?.
MDL  Moldovan Leu  leu
MGA  Malagasy Ariary  ariary
MKD  Macedonian Denar  ден
MMK  Myanma Kyat  K
MNT  Mongolian T?gr?g (Tugrik)  ?
MOP  Macanese Pataca  毫
MRO  Mauritanian Ouguiya  UM
MUR  Mauritian Rupee  R
MVR  Maldivian Rufiyaa  .?
MWK  Malawian Kwacha  MK
MXN  Mexican Peso  $
MYR  Malaysian Ringgit  RM
MZN  Mozambican Metical  MTn
NAD  Namibian Dollar  $
NGN  Nigerian Naira  ?
NIO  Nicaraguan Cordoba Oro  C$
NOK  Norwegian Krone  kr
NPR  Nepalese Rupee  ??.
NZD  New Zealand Dollar  $
OMR  Omani Rial  ?.?.
PAB  Panamanian Balboa  ?
PEN  Peruvian Nuevo Sol  S/.
PGK  Papua New Guinean Kina  K
PHP  Philippine Peso  ?
PKR  Pakistani Rupee  Rs
PLN  Polish Zloty  z?
PYG  Paraguayan Guaraní  ?
QAR  Qatari Rial  ?.?
RON  Romanian New Leu  lei
RSD  Serbian Dinar  РСД
RUB  Russian Rouble  руб
RWF  Rwandan Franc  RF
SAR  Saudi Riyal  ?.?
SBD  Solomon Islands Dollar  $
SCR  Seychelles Rupee  SRe
SDG  Sudanese Pound  SDG
SEK  Swedish Krona  kr
SGD  Singapore Dollar  $
SHP  Saint Helena Pound  £
SLL  Sierra Leonean Leone  Le
SOS  Somali Shilling  So. Sh.
SRD  Surinamese Dollar  $
STD  S?o Tomé and Príncipe Dobra  Db
SYP  Syrian Pound  SYP
SZL  Lilangeni  E
THB  Thai Baht  ?
TJS  Tajikistani Somoni  TJS
TMT  Turkmenistani Manat  m
TND  Tunisian Dinar  ?.?
TOP  Tongan Pa?anga  T$
TRY  Turkish Lira  TL
TTD  Trinidad and Tobago Dollar  $
TWD  New Taiwan Dollar  $
TZS  Tanzanian Shilling  TZS
UAH  Ukrainian Hryvnia  ?
UGX  Ugandan Shilling  Ush
USD  United States Dollar  $
UYU  Uruguayan Peso  $
UZS  Uzbekistan Som  som
VEF  Venezuelan Bolívar Fuerte  Bs. F
VND  Vietnamese D?ng  ?
VUV  Vanuatu Vatu  Vt
WST  Samoan Tala  WS$
XAF  CFA Franc BEAC  FCFA
XCD  East Caribbean Dollar  $
XOF  CFA Franc BCEAO  CFA
XPF  CFP Franc  F
YER  Yemeni Rial  rial
ZAR  South African Rand  R
ZMK  Zambian Kwacha  ZK
ZWL  Zimbabwe Dollar  $

Oh look. Zimbabwe had their own dollar the last time I updated. Which can happen quicker, you drop it from the list, or a framework is updated to do so?

哦,看。我上次更新时津巴布韦有自己的美元。哪个可以更快地发生,您将其从列表中删除,还是更新框架以这样做?