Skip to content

Commit 2411bcd

Browse files
committed
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.
1 parent 20b45d0 commit 2411bcd

File tree

2 files changed

+10
-50
lines changed

2 files changed

+10
-50
lines changed

data.go

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen.go

-40
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ func main() {
183183
}
184184

185185
log.Println("Processing...")
186-
err = replaceDigits(currencies, assetDir)
187-
if err != nil {
188-
os.RemoveAll(assetDir)
189-
log.Fatal(err)
190-
}
191186
locales, err := collectLocales(assetDir)
192187
if err != nil {
193188
os.RemoveAll(assetDir)
@@ -331,8 +326,6 @@ func fetchISO() (map[string]*currencyInfo, error) {
331326
continue
332327
}
333328

334-
// We use ISO digits here with a fallback to 2, but prefer CLDR
335-
// data when available. See replaceDigits() for the next step.
336329
digits := parseDigits(entry.Digits, 2)
337330
currencies[entry.Code] = &currencyInfo{entry.Number, digits}
338331
}
@@ -381,39 +374,6 @@ func collectLocales(dir string) ([]string, error) {
381374
return locales, nil
382375
}
383376

384-
// replaceDigits replaces currency digits with data from CLDR.
385-
//
386-
// CLDR data reflects real life usage more closely, specifying 0 digits
387-
// (instead of 2 in ISO data) for ~14 currencies, such as ALL and RSD.
388-
//
389-
// Note that CLDR does not have data for every currency, in which ase
390-
// the original ISO digits are kept.
391-
func replaceDigits(currencies map[string]*currencyInfo, dir string) error {
392-
data, err := os.ReadFile(dir + "/cldr-json/cldr-core/supplemental/currencyData.json")
393-
if err != nil {
394-
return fmt.Errorf("replaceDigits: %w", err)
395-
}
396-
aux := struct {
397-
Supplemental struct {
398-
CurrencyData struct {
399-
Fractions map[string]map[string]string
400-
}
401-
}
402-
}{}
403-
if err := json.Unmarshal(data, &aux); err != nil {
404-
return fmt.Errorf("replaceDigits: %w", err)
405-
}
406-
407-
for currencyCode := range currencies {
408-
fractions, ok := aux.Supplemental.CurrencyData.Fractions[currencyCode]
409-
if ok {
410-
currencies[currencyCode].digits = parseDigits(fractions["_digits"], 2)
411-
}
412-
}
413-
414-
return nil
415-
}
416-
417377
// generateCountryCurrencies generates the map of country codes to currency codes.
418378
func generateCountryCurrencies(dir string) (map[string]string, error) {
419379
data, err := os.ReadFile(dir + "/cldr-json/cldr-core/supplemental/currencyData.json")

0 commit comments

Comments
 (0)