Skip to content

Commit

Permalink
Stop overriding ISO digits with CLDR digits.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bojanz committed Nov 3, 2024
1 parent 20b45d0 commit 2411bcd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 50 deletions.
20 changes: 10 additions & 10 deletions data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 0 additions & 40 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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] = &currencyInfo{entry.Number, digits}
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 2411bcd

Please sign in to comment.