Golang中的货币格式使用来自golang.org/x/text/currency的currency.Symbol

时间:2017-12-13 19:48:40

标签: go currency-formatting

您好我正在使用一种函数来格式化货币值。我正在使用golang.org/x/text/currency来完成这项工作,但是我在逗号处有点输出而没有数千个分隔符。

func (produto *Produto) FormataPreco(valor int64) string {
    unit, _ := currency.ParseISO("BRL")
    p := message.NewPrinter(language.BrazilianPortuguese)
    return p.Sprint(currency.Symbol(unit.Amount(float64(valor) / 100)))
}

预期结果应该是R $ 123.456,78,但我得到R $ 123456.78

---编辑---

我使用硬编码值创建了一个版本,但我想要一个使用系统区域设置资源的解决方案。

func (produto *Produto) FormataPreco(valor int64) string {
    p := message.NewPrinter(language.BrazilianPortuguese)
    return p.Sprintf("R$ %.2f", float64(valor/100))
}

1 个答案:

答案 0 :(得分:1)

在此示例中,我从语言代码推断出货币格式。

https://goplay.space/#fqs9t8MG062

n := display.Tags(language.English)
for _, lcode := range []string{"en_US", "pt_BR", "de", "ja", "hi"} {
    lang := language.MustParse(lcode)
    cur, _ := currency.FromTag(lang)
    scale, _ := currency.Cash.Rounding(cur) // fractional digits
    dec := number.Decimal(100000.00, number.Scale(scale))
    p := message.NewPrinter(lang)
    p.Printf("%24v (%v): %v%v\n", n.Name(lang), cur, currency.Symbol(cur), dec)
}

//         American English (USD): $100,000.00
//     Brazilian Portuguese (BRL): R$100.000,00
//                   German (EUR): €100.000,00
//                 Japanese (JPY): ¥100,000
//                    Hindi (INR): ₹1,00,000.00

您还可以解析ISO货币代码,但是随后还必须指定格式化数字的语言。输出语言不会影响小数位数,但 会影响使用逗号和句点的位置:

https://goplay.space/#DlxSmjZbHH6

for _, iso := range []string{"USD", "BRL", "EUR", "JPY", "INR"} {
    cur := currency.MustParseISO(iso)
    scale, _ := currency.Cash.Rounding(cur) // fractional digits
    dec := number.Decimal(100000.00, number.Scale(scale))
    p := message.NewPrinter(language.English)
    p.Printf("%v: %v%v\n", cur, currency.Symbol(cur), dec)
}

// USD: $100,000.00
// BRL: R$100,000.00
// EUR: €100,000.00
// JPY: ¥100,000
// INR: ₹100,000.00

某些货币以四舍五入为单位,例如0.05或0.50。在这种情况下,第二个返回值currency.Cash.Rounding(cur)将返回5或50而不是1。要为Decimal格式化程序提供期望的IncrementString,我们需要做更多处理:

package main

import (
    "math"
    "strconv"

    "golang.org/x/text/currency"
    "golang.org/x/text/language"
    "golang.org/x/text/language/display"
    "golang.org/x/text/message"
    "golang.org/x/text/number"
)

func main() {
    n := display.Tags(language.English)
    for _, lcode := range []string{"en_US", "en_CA", "da", "ja"} {
        lang := language.MustParse(lcode)
        cur, _ := currency.FromTag(lang)
        scale, incCents := currency.Cash.Rounding(cur) // fractional digits
        incFloat := math.Pow10(-scale) * float64(incCents)
        incFmt := strconv.FormatFloat(incFloat, 'f', scale, 64)
        dec := number.Decimal(100000.26,
            number.Scale(scale), number.IncrementString(incFmt))
        p := message.NewPrinter(lang)
        p.Printf("%24v %v, %4s-rounding: %3v%v\n",
            n.Name(lang), cur, incFmt, currency.Symbol(cur), dec)
    }
}

//    American English USD, 0.01-rounding: $100,000.26
//    Canadian English CAD, 0.05-rounding: CA$100,000.25
//              Danish DKK, 0.50-rounding: DKK100.000,50
//            Japanese JPY,    1-rounding: ¥100,000