Skip to content

Golang: Format Thousand Separator for Currency

There is a super easy trick to format a number with thousand separator in any language.

package main

import (
	"fmt"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

func main() {

        const myNumber = 370000

        // English currency formatting
	em := message.NewPrinter(language.English)
	var enNumber string = em.Sprintf("%d", myNumber)
	fmt.Printf("%#v\n", enNumber)

        // Greek currency formatting
	gk := message.NewPrinter(language.Greek)
	var gkNumber string = gk.Sprintf("%d", myNumber)
	fmt.Printf("%#v\n", gkNumber)

        // Arabic currency formatting
	ar := message.NewPrinter(language.Arabic)
	var arNumber string = ar.Sprintf("%d", myNumber)
	fmt.Printf("%#v\n", arNumber)

	// Output:
	// "370,000"
	// "370.000"
	// "٣٧٠٬٠٠٠"
}

Leave a Reply

Your email address will not be published. Required fields are marked *