Does Go's fmt.Printf
support outputting a number with the thousands comma?
fmt.Printf("%d", 1000)
outputs 1000
, what format can I specify to output 1,000
instead?
The docs don't seem to mention commas, and I couldn't immediately see anything in the source.
I wrote a library for this as well as a few other human-representation concerns.
Example results:
Example Usage:
If you don't want to use a library (for whatever reason), I knocked this up. It seems to work and can use any specified rune as a delimiter:
Note: after further testing, this function doesn't work perfectly. I would suggest using the solution posted by @IvanTung, and welcome any edits from anyone who can get mine to work perfectly.
Use
golang.org/x/text/message
to print using localized formatting for any language in the Unicode CLDR: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
Here's a simple function using regex:
Example:
Output:
https://play.golang.org/p/0v6wOzxJ1H