How to fmt.Printf an integer with thousands comma

后端 未结 13 1857
独厮守ぢ
独厮守ぢ 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 21:59

    You can also use this small package: https://github.com/floscodes/golang-thousands.

    Just convert your number to a string an then use the Separate-function like this:

    n:="3478686" // your number as a string
    
    thousands.Separate(n, "en") // adds thousands separators. the second argument sets the language mode.
    

提交回复
热议问题