"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Handle Currency Formatting in Golang?

How to Handle Currency Formatting in Golang?

Published on 2024-11-08
Browse:985

How to Handle Currency Formatting in Golang?

Proper Currency Formatting in Golang

When working with currency formatting, it's essential to consider the specific requirements for each currency and language combination. Using golang.org/x/text/currency, you can easily format values based on local conventions. However, you may encounter some challenges in getting the desired output.

Using Currency Symbols

In your code, you use currency.Symbol to retrieve the currency symbol for the specified ISO code. However, when using this method without providing a language context, you might not get the expected separators or number formatting.

Incorporating Local Formats

Instead of attempting to manually create your format, you can leverage the display and message packages to automatically retrieve the appropriate format based on the language and region. Here's an example:

import (
    "fmt"

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

func formatCurrency(value int64, currencyCode string, languageCode string) string {
    lang, _ := language.Parse(languageCode)
    p := message.NewPrinter(lang)

    unit, _ := currency.ParseISO(currencyCode)
    return p.Sprintf("%v%v", currency.Symbol(unit), float64(value))
}

This function takes in a value, currency code, and language code and returns a properly formatted currency string.

Handling Increment Units

Some currencies have a fixed increment unit, such as 5 or 10 cents. To account for this, you can use the number package to specify the increment:

import (
    "golang.org/x/text/currency"
    "golang.org/x/text/number"
)

func formatWithIncrement(value int64, currencyCode string, increment int) string {
    unit, _ := currency.ParseISO(currencyCode)
    scale, inc := currency.Cash.Rounding(unit)
    dec := number.Decimal(float64(value), number.Scale(scale), number.IncrementString(fmt.Sprintf("%.2f", float64(inc))))
    return fmt.Sprintf("%v%v", currency.Symbol(unit), dec)
}

By providing the language context, inferring the currency format from language codes, and considering increment units, you can create flexible and accurate currency formatting solutions in Golang using golang.org/x/text/currency.

Release Statement This article is reprinted at: 1729688989 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3