How to fmt.Printf an integer with thousands comma

后端 未结 13 1881
独厮守ぢ
独厮守ぢ 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:49

    I wrote a library for this as well as a few other human-representation concerns.

    Example results:

    0 -> 0
    100 -> 100
    1000 -> 1,000
    1000000000 -> 1,000,000,000
    -100000 -> -100,000
    

    Example Usage:

    fmt.Printf("You owe $%s.\n", humanize.Comma(6582491))
    

提交回复
热议问题