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
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();
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With