Go 的货币处理
处理货币金额,提供货币信息和格式化。 由 CLDR v48 提供支持,仅占 ~40kb 的数据。 背景故事: https://bojanz.github.io/price-currency-handling-go/
amount, _ := currency.NewAmount("275.98", "EUR")
total, _ := amount.Mul("4")
installments, _ := total.Split(3) // 367.98, 367.97, 367.97
locale := currency.NewLocale("fr")
formatter := currency.NewFormatter(locale)
fmt.Println(formatter.Format(total)) // 1 103,92 €
// Convert the amount to Iranian rial and show it in Farsi.
total, _ = total.Convert("IRR", "45.538")
total = total.Round()
locale = currency.NewLocale("fa")
formatter = currency.NewFormatter(locale)
fmt.Println(formatter.Format(total)) // ریال ۵۰٬۲۷۰货币金额不能为浮点数。一旦有多种货币(在数据库中很难排序),或者需要小于最小单位的精度(由于商家或税收要求等),则存储整数小数单位(2.99 => 299)就会变得困难。因此需要一个真正的任意精度的小数类型。由于 Go 没有此类类型,因此使用了用户空间实现,由 cockroachdb/apd 包提供。Amount 结构提供了一个易于使用的抽象,允许将底层实现在未来更换而不破坏向后兼容性。
使用了 CLDR 地区的"现代"子集,从而减少了列表的长度。
暂无开放 Issues,或尚未同步最近议题。