How to fmt.Printf an integer with thousands comma

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

    I published a Go snippet over at Github of a function to render a number (float64 or int) according to user-specified thousand separator, decimal separator and decimal precision.

    https://gist.github.com/gorhill/5285193

    Usage: s := RenderFloat(format, n)
    
    The format parameter tells how to render the number n.
    
    Examples of format strings, given n = 12345.6789:
    
    "#,###.##" => "12,345.67"
    "#,###." => "12,345"
    "#,###" => "12345,678"
    "#\u202F###,##" => "12 345,67"
    "#.###,###### => 12.345,678900
    "" (aka default format) => 12,345.67
    

提交回复
热议问题