How to fmt.Printf an integer with thousands comma

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

    I got interested in the performance of solutions offered in earlier answers and wrote tests with benchmarks for them, including two code snippets of mine. The following results were measured on MacBook 2018, i7 2.6GHz:

    +---------------------+-------------------------------------------+--------------+
    |       Author        |                Description                |    Result    |
    |---------------------|-------------------------------------------|--------------|
    | myself              | dividing by 1,000 and appending groups    |  3,472 ns/op |
    | myself              | inserting commas to digit groups          |  2,662 ns/op |
    | @icza               | collecting digit by digit to output array |  1,695 ns/op |
    | @dolmen             | copying digit groups to output array      |  1,797 ns/op |
    | @Ivan Tung          | writing digit by digit to buffer          |  2,753 ns/op |
    | @jchavannes         | inserting commas using a regexp           | 63,995 ns/op |
    | @Steffi Keran Rani, | using github.com/dustin/go-humanize       |  3,525 ns/op |
    |  @abourget, @Dustin |                                           |              |
    | @dolmen             | using golang.org/x/text/message           | 12,511 ns/op |
    +---------------------+-------------------------------------------+--------------+
    
    • If you want the fastest solution, grab @icza's code snippet. Although it goes digit by digit and not by groups of three digits, it emerged as the fastest.
    • If you want the shortest reasonable code snippet, look at mine below. It adds more than half of the time of the fastest solution, but the code is three times shorter.
    • If you want a one-liner and do not mind using an external library, go for github.com/dustin/go-humanize. It is more than twice slower as the fastest solution, but the library might help you with other formatting.
    • If you want localized output, choose golang.org/x/text/message. It is seven times slower than the fastest solution, but the luxury of matching the consumer's language does not come free.

    Other hand-coded solutions are fast too and you will not regret choosing any of them, except for the usage of regexp. Using regexp needs the shortest code snippet, but the performance is so tragic, that it is not worth it.

    My contribution to this topic, which you can try running in the playground:

    func formatInt(number int) string {
        output := strconv.Itoa(number)
        startOffset := 3
        if number < 0 {
            startOffset++
        }
        for outputIndex := len(output); outputIndex > startOffset; {
            outputIndex -= 3
            output = output[:outputIndex] + "," + output[outputIndex:]
        }
        return output
    }
    

提交回复
热议问题