From 2411bcd1a423b04290f0b75bbe1e67f41ad86f70 Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Sun, 3 Nov 2024 12:04:55 +0100 Subject: [PATCH] Stop overriding ISO digits with CLDR digits. For ~14 currencies ISO specifies 2 or 3 digits, but CLDR specifies 0, because that better reflects real world usage, where the currency's minor units no longer have value and aren't used in cash transactions. However, it is common in such cases for the digits to still be used for banking transactions, tax amounts, etc. Since those represent common use cases for this package, it is safer to default to the ISO digits, and let consumers further restrict and round as needed. Fixes #37. --- data.go | 20 ++++++++++---------- gen.go | 40 ---------------------------------------- 2 files changed, 10 insertions(+), 50 deletions(-) diff --git a/data.go b/data.go index 87657ab..a2ee32c 100644 --- a/data.go +++ b/data.go @@ -65,7 +65,7 @@ var currencyCodes = []string{ } var currencies = map[string]currencyInfo{ - "AED": {"784", 2}, "AFN": {"971", 0}, "ALL": {"008", 0}, + "AED": {"784", 2}, "AFN": {"971", 2}, "ALL": {"008", 2}, "AMD": {"051", 2}, "ANG": {"532", 2}, "AOA": {"973", 2}, "ARS": {"032", 2}, "AUD": {"036", 2}, "AWG": {"533", 2}, "AZN": {"944", 2}, "BAM": {"977", 2}, "BBD": {"052", 2}, @@ -87,15 +87,15 @@ var currencies = map[string]currencyInfo{ "GTQ": {"320", 2}, "GYD": {"328", 2}, "HKD": {"344", 2}, "HNL": {"340", 2}, "HTG": {"332", 2}, "HUF": {"348", 2}, "IDR": {"360", 2}, "ILS": {"376", 2}, "INR": {"356", 2}, - "IQD": {"368", 0}, "IRR": {"364", 0}, "ISK": {"352", 0}, + "IQD": {"368", 3}, "IRR": {"364", 2}, "ISK": {"352", 0}, "JMD": {"388", 2}, "JOD": {"400", 3}, "JPY": {"392", 0}, "KES": {"404", 2}, "KGS": {"417", 2}, "KHR": {"116", 2}, - "KMF": {"174", 0}, "KPW": {"408", 0}, "KRW": {"410", 0}, + "KMF": {"174", 0}, "KPW": {"408", 2}, "KRW": {"410", 0}, "KWD": {"414", 3}, "KYD": {"136", 2}, "KZT": {"398", 2}, - "LAK": {"418", 0}, "LBP": {"422", 0}, "LKR": {"144", 2}, + "LAK": {"418", 2}, "LBP": {"422", 2}, "LKR": {"144", 2}, "LRD": {"430", 2}, "LSL": {"426", 2}, "LYD": {"434", 3}, - "MAD": {"504", 2}, "MDL": {"498", 2}, "MGA": {"969", 0}, - "MKD": {"807", 2}, "MMK": {"104", 0}, "MNT": {"496", 2}, + "MAD": {"504", 2}, "MDL": {"498", 2}, "MGA": {"969", 2}, + "MKD": {"807", 2}, "MMK": {"104", 2}, "MNT": {"496", 2}, "MOP": {"446", 2}, "MRU": {"929", 2}, "MUR": {"480", 2}, "MVR": {"462", 2}, "MWK": {"454", 2}, "MXN": {"484", 2}, "MXV": {"979", 2}, "MYR": {"458", 2}, "MZN": {"943", 2}, @@ -104,12 +104,12 @@ var currencies = map[string]currencyInfo{ "OMR": {"512", 3}, "PAB": {"590", 2}, "PEN": {"604", 2}, "PGK": {"598", 2}, "PHP": {"608", 2}, "PKR": {"586", 2}, "PLN": {"985", 2}, "PYG": {"600", 0}, "QAR": {"634", 2}, - "RON": {"946", 2}, "RSD": {"941", 0}, "RUB": {"643", 2}, + "RON": {"946", 2}, "RSD": {"941", 2}, "RUB": {"643", 2}, "RWF": {"646", 0}, "SAR": {"682", 2}, "SBD": {"090", 2}, "SCR": {"690", 2}, "SDG": {"938", 2}, "SEK": {"752", 2}, "SGD": {"702", 2}, "SHP": {"654", 2}, "SLE": {"925", 2}, - "SOS": {"706", 0}, "SRD": {"968", 2}, "SSP": {"728", 2}, - "STN": {"930", 2}, "SVC": {"222", 2}, "SYP": {"760", 0}, + "SOS": {"706", 2}, "SRD": {"968", 2}, "SSP": {"728", 2}, + "STN": {"930", 2}, "SVC": {"222", 2}, "SYP": {"760", 2}, "SZL": {"748", 2}, "THB": {"764", 2}, "TJS": {"972", 2}, "TMT": {"934", 2}, "TND": {"788", 3}, "TOP": {"776", 2}, "TRY": {"949", 2}, "TTD": {"780", 2}, "TWD": {"901", 2}, @@ -119,7 +119,7 @@ var currencies = map[string]currencyInfo{ "VED": {"926", 2}, "VES": {"928", 2}, "VND": {"704", 0}, "VUV": {"548", 0}, "WST": {"882", 2}, "XAF": {"950", 0}, "XCD": {"951", 2}, "XOF": {"952", 0}, "XPF": {"953", 0}, - "YER": {"886", 0}, "ZAR": {"710", 2}, "ZMW": {"967", 2}, + "YER": {"886", 2}, "ZAR": {"710", 2}, "ZMW": {"967", 2}, "ZWG": {"924", 2}, } diff --git a/gen.go b/gen.go index 9fef26e..60ea15a 100644 --- a/gen.go +++ b/gen.go @@ -183,11 +183,6 @@ func main() { } log.Println("Processing...") - err = replaceDigits(currencies, assetDir) - if err != nil { - os.RemoveAll(assetDir) - log.Fatal(err) - } locales, err := collectLocales(assetDir) if err != nil { os.RemoveAll(assetDir) @@ -331,8 +326,6 @@ func fetchISO() (map[string]*currencyInfo, error) { continue } - // We use ISO digits here with a fallback to 2, but prefer CLDR - // data when available. See replaceDigits() for the next step. digits := parseDigits(entry.Digits, 2) currencies[entry.Code] = ¤cyInfo{entry.Number, digits} } @@ -381,39 +374,6 @@ func collectLocales(dir string) ([]string, error) { return locales, nil } -// replaceDigits replaces currency digits with data from CLDR. -// -// CLDR data reflects real life usage more closely, specifying 0 digits -// (instead of 2 in ISO data) for ~14 currencies, such as ALL and RSD. -// -// Note that CLDR does not have data for every currency, in which ase -// the original ISO digits are kept. -func replaceDigits(currencies map[string]*currencyInfo, dir string) error { - data, err := os.ReadFile(dir + "/cldr-json/cldr-core/supplemental/currencyData.json") - if err != nil { - return fmt.Errorf("replaceDigits: %w", err) - } - aux := struct { - Supplemental struct { - CurrencyData struct { - Fractions map[string]map[string]string - } - } - }{} - if err := json.Unmarshal(data, &aux); err != nil { - return fmt.Errorf("replaceDigits: %w", err) - } - - for currencyCode := range currencies { - fractions, ok := aux.Supplemental.CurrencyData.Fractions[currencyCode] - if ok { - currencies[currencyCode].digits = parseDigits(fractions["_digits"], 2) - } - } - - return nil -} - // generateCountryCurrencies generates the map of country codes to currency codes. func generateCountryCurrencies(dir string) (map[string]string, error) { data, err := os.ReadFile(dir + "/cldr-json/cldr-core/supplemental/currencyData.json")