将整数转换为货币

发布于 2024-12-28 04:47:15 字数 144 浏览 0 评论 0原文

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

分段阅读_第 2130 章21.30 英镑
分段阅读_第 2130 章$21.30
ETC

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?

2130 -> £21.30
2130 -> $21.30
etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

檐上三寸雪 2025-01-04 04:47:15
int valueFromDb = 2130;
decimal result = valueFromDb / 100m;
string formatted = result.ToString("c");

可以快速扩展方法化:

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

这样你现在就可以变得更干燥:

int valueFromDb = 2130;
string formatted = valueFromDb.AsCurrency();
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();
雪花飘飘的天空 2025-01-04 04:47:15
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
}

然而。这意味着 123.45 英镑变成了 123.45 美元或 123.45 欧元,这显然是不正确的。

还有一个问题是,比索/美元符号 $ 可以用来表示 NIO、AUD、CAD、TOP、USD、HKD 等等。它并不是唯一一个在多个地方使用的符号。如果您记录的是旧付款,甚至用于 IEP 和 ITL(不再使用)的英镑之类的事情也可能成为问题。

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

货币价值仅对于隐含或显式货币才有意义。在这里,您要么采用一个没有的逻辑,要么用显式替换隐含的逻辑,要么依赖一大段压倒一切的逻辑来保持正确。

其中最后一个可以发挥作用,但也令人担忧。另外两个是错误的。

最后,文化最多只能告诉你人们最熟悉什么货币,但不能告诉你他们总是使用什么。我经常使用 kroner 和 øre,但在使用它们时我仍然使用 Hiberno-English。

我建议将 ISO 4217 代码与货币一起存储。最好将其也包含在最终发票上。符号有利于美观,代码有利于使事情精确。

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

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 Ðồ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  $

哦,看。我上次更新时津巴布韦有自己的美元。哪个会发生得更快,您将其从列表中删除,或者更新框架来做到这一点?

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.

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.

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.

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.

Ignore the user's culture, except perhaps as a best-guess at a default choice. (e.g. if they're American then they probably want 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 Ðồ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?

祁梦 2025-01-04 04:47:15

以下是在 C# 中使用 string.format 执行此操作的方法:

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

这将为您提供当前区域性的格式。 有关 MSDN 的详细信息

Here's how to do it in c# with 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

羁〃客ぐ 2025-01-04 04:47:15

请尝试以下操作:

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

Try the following:

 decimal amount = 2130 / 100m;
 string output = value.ToString("C", CultureInfo.CurrentCulture);
长梦不多时 2025-01-04 04:47:15
decimal value = ((decimal)(myNumber / 100.0));
var result = String.Format("{0:c}", value);
decimal value = ((decimal)(myNumber / 100.0));
var result = String.Format("{0:c}", value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文