That would be what the humanize.FormatFloat() does:
// FormatFloat produces a formatted number as string based on the following user-specified criteria:
// * thousands separator
// * decimal separator
// * decimal precision
In your case:
FormatFloat("#,###.##", afloat)
That being said, as commented by LenW, float
(in Go, float64
) is not a good fit for currency.
See floating-point-gui.de.
Using a package like go-inf/inf
(previously go/dec
, used for instance in this currency implementation) is better.
See Dec.go:
// A Dec represents a signed arbitrary-precision decimal.
// It is a combination of a sign, an arbitrary-precision integer coefficient
// value, and a signed fixed-precision exponent value.
// The sign and the coefficient value are handled together as a signed value
// and referred to as the unscaled value.
That type Dec
does include a Format()
method.
Since July 2015, you now have leekchan/accounting
from Kyoung-chan Lee (leekchan
) with the same advice:
Please do not use float64
to count money. Floats can have errors when you perform operations on them.
Using big.Rat
(< Go 1.5) or big.Float
(>= Go 1.5) is highly recommended. (accounting supports float64
, but it is just for convenience.)
fmt.Println(ac.FormatMoneyBigFloat(big.NewFloat(123456789.213123))) // "$123,456,789.21"