Last active
July 18, 2019 12:01
-
-
Save huandu/d732cbbecfb68419013d5e2746d3dd36 to your computer and use it in GitHub Desktop.
Show currency in different locales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/text/currency" | |
"golang.org/x/text/language" | |
"golang.org/x/text/message" | |
"golang.org/x/text/number" | |
) | |
// CurrencyFormat defines all options to format currency name. | |
type CurrencyFormat struct { | |
Unit currency.Unit | |
Display string | |
} | |
// CF makes a CurrencyFormat. | |
func CF(unit string, display string) *CurrencyFormat { | |
return &CurrencyFormat{ | |
Unit: currency.MustParseISO(unit), | |
Display: display, | |
} | |
} | |
var ( | |
cnZH = language.Make("cn-ZH") | |
enUS = language.Make("en-US") | |
esMX = language.Make("es-MX") | |
enAU = language.Make("en-AU") | |
jaJP = language.Make("ja-JP") | |
esPE = language.Make("es-PE") | |
currencies = map[language.Tag][]*CurrencyFormat{ | |
cnZH: { | |
CF("CNY", "%m元"), | |
CF("USD", "$%m"), | |
CF("MXN", "$%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "%m円"), | |
}, | |
enUS: { | |
CF("CNY", "¥%m"), | |
CF("USD", "$%m"), | |
CF("MXN", "MXN $%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "¥%m"), | |
}, | |
esMX: { | |
CF("CNY", "¥%m"), | |
CF("USD", "$%m"), | |
CF("MXN", "MXN $%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "¥%m"), | |
}, | |
enAU: { | |
CF("CNY", "¥%m"), | |
CF("USD", "$%m"), | |
CF("MXN", "MXN $%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "¥%m"), | |
}, | |
jaJP: { | |
CF("CNY", "¥%m"), | |
CF("USD", "$%m"), | |
CF("MXN", "MXN $%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "%m円"), | |
}, | |
esPE: { | |
CF("CNY", "¥%m"), | |
CF("USD", "$%m"), | |
CF("MXN", "MXN $%m"), | |
CF("AUD", "$%m"), | |
CF("PEN", "S/ $%m"), | |
CF("JPY", "¥%m"), | |
}, | |
} | |
// PEN is Peruvian sol. | |
PEN = currency.MustParseISO("PEN") | |
currencyFraction = map[currency.Unit]int{ | |
currency.CNY: 2, | |
currency.USD: 2, | |
currency.MXN: 2, | |
currency.AUD: 2, | |
PEN: 2, | |
currency.JPY: 0, | |
} | |
) | |
func init() { | |
for locale, formats := range currencies { | |
for _, f := range formats { | |
message.SetString(locale, f.Unit.String()+" %m", f.Display) | |
} | |
} | |
} | |
func formatCurrency(locale string, unit string, value interface{}) string { | |
tag := language.Make(locale) | |
printer := message.NewPrinter(tag) | |
cu := currency.MustParseISO(unit) | |
frac := currencyFraction[cu] | |
format := unit + " %m" | |
opts := []number.Option{ | |
number.IncrementString(fmt.Sprintf("0.%0[1]*d", frac+1, 5)), | |
number.MaxFractionDigits(frac), | |
number.MinFractionDigits(frac), | |
} | |
num := printer.Sprint(number.Decimal(value, opts...)) | |
return printer.Sprintf(format, num) | |
} | |
func main() { | |
for _, unit := range []string{"CNY", "USD", "MXN", "AUD", "PEN", "JPY"} { | |
for _, locale := range []string{"cn-ZH", "en-US", "es-MX", "en-AU", "ja-JP", "es-PE"} { | |
fmt.Println(unit, locale, formatCurrency(locale, unit, 123456.789)) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run main.go | |
CNY cn-ZH 123,456.79元 | |
CNY en-US ¥123,456.79 | |
CNY es-MX ¥123,456.79 | |
CNY en-AU ¥123,456.79 | |
CNY ja-JP ¥123,456.79 | |
CNY es-PE ¥123,456.79 | |
USD cn-ZH $123,456.79 | |
USD en-US $123,456.79 | |
USD es-MX $123,456.79 | |
USD en-AU $123,456.79 | |
USD ja-JP $123,456.79 | |
USD es-PE $123,456.79 | |
MXN cn-ZH $123,456.79 | |
MXN en-US MXN $123,456.79 | |
MXN es-MX MXN $123,456.79 | |
MXN en-AU MXN $123,456.79 | |
MXN ja-JP MXN $123,456.79 | |
MXN es-PE MXN $123,456.79 | |
AUD cn-ZH $123,456.79 | |
AUD en-US $123,456.79 | |
AUD es-MX $123,456.79 | |
AUD en-AU $123,456.79 | |
AUD ja-JP $123,456.79 | |
AUD es-PE $123,456.79 | |
PEN cn-ZH S/ $123,456.79 | |
PEN en-US S/ $123,456.79 | |
PEN es-MX S/ $123,456.79 | |
PEN en-AU S/ $123,456.79 | |
PEN ja-JP S/ $123,456.79 | |
PEN es-PE S/ $123,456.79 | |
JPY cn-ZH 123,457円 | |
JPY en-US ¥123,457 | |
JPY es-MX ¥123,457 | |
JPY en-AU ¥123,457 | |
JPY ja-JP 123,457円 | |
JPY es-PE ¥123,457 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment