How to fmt.Printf an integer with thousands comma

后端 未结 13 1809
独厮守ぢ
独厮守ぢ 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
    
    0 讨论(0)
  • 2020-12-04 22:01
    import ("fmt"; "strings")
    
    func commas(s string) string {
        if len(s) <= 3 {
            return s
        } else {
            return commas(s[0:len(s)-3]) + "," + s[len(s)-3:]
        }
    }
    
    func toString(f float64) string {
        parts := strings.Split(fmt.Sprintf("%.2f", f), ".")
        if parts[0][0] == '-' {
            return "-" + commas(parts[0][1:]) + "." + parts[1]
        }
        return commas(parts[0]) + "." + parts[1]
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-04 22:03

    The package humanize can do the magic! Refer the documentation of this package here. To use this package, install it first by using a tool like Git SCM. If you are using Git Bash, open the shell window and type:

    go get -u github.com/dustin/go-humanize
    

    Once this is done, you can use the following solution code (Say, main.go):

    package main
    
    import (
        "fmt"
        "github.com/dustin/go-humanize"
    )
    
    func main() {
        fmt.Println(humanize.Commaf(float64(123456789)));
        fmt.Println(humanize.Commaf(float64(-1000000000)));
        fmt.Println(humanize.Commaf(float64(-100000.005)));
        fmt.Println(humanize.Commaf(float64(100000.000)));
    }
    

    There are other variations to Commaf like BigComma, Comma, BigCommaf etc. which depends on the data type of your input.

    So, on running this program using the command:

    go run main.go
    

    You will see an output such as this:

    123,456,789
    -1,000,000,000
    -100,000.005
    100,000
    
    0 讨论(0)
  • 2020-12-04 22:07

    Use https://github.com/dustin/go-humanize .. it has a bunch of helpers to deal with those things. In addition to bytes as MiB, MB, and other goodies.

    0 讨论(0)
  • 2020-12-04 22:08

    Here's a simple function using regex:

    import (
        "regexp"
    )
    
    func formatCommas(num int) string {
        str := fmt.Sprintf("%d", num)
        re := regexp.MustCompile("(\\d+)(\\d{3})")
        for n := ""; n != str; {
            n = str
            str = re.ReplaceAllString(str, "$1,$2")
        }
        return str
    }
    

    Example:

    fmt.Println(formatCommas(1000))
    fmt.Println(formatCommas(-1000000000))
    

    Output:

    1,000
    -1,000,000,000
    

    https://play.golang.org/p/vnsAV23nUXv

    0 讨论(0)
提交回复
热议问题