How to fmt.Printf an integer with thousands comma

后端 未结 13 1894
独厮守ぢ
独厮守ぢ 2020-12-04 21:21

Does Go\'s fmt.Printf support outputting a number with the thousands comma?

fmt.Printf(\"%d\", 1000) outputs 1000, what format

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 22:03

    Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR:

    package main
    
    import (
        "golang.org/x/text/language"
        "golang.org/x/text/message"
    )
    
    func main() {
        p := message.NewPrinter(language.English)
        p.Printf("%d\n", 1000)
    
        // Output:
        // 1,000
    }
    

提交回复
热议问题