Adds thousand separators (commas) to numbers, cleans up float representations by removing trailing zeros, and provides custom formatting with configurable separators.
import "github.com/dustin/go-humanize"func Comma(v int64) stringAdds commas as thousand separators to an int64 value.
Parameters:
v - The integer to formatReturns: String with commas every three digits
Example:
humanize.Comma(834142) // "834,142"
humanize.Comma(1000) // "1,000"
humanize.Comma(1000000000) // "1,000,000,000"
humanize.Comma(-100000) // "-100,000"func Commaf(v float64) stringAdds commas as thousand separators to a float64 value, preserving decimal places.
Parameters:
v - The float to formatReturns: String with commas every three digits in the integer part
Example:
humanize.Commaf(834142.32) // "834,142.32"
humanize.Commaf(1000.5) // "1,000.5"
humanize.Commaf(12345678.901) // "12,345,678.901"func CommafWithDigits(f float64, decimals int) stringAdds commas to a float64 and limits the decimal places to the specified number.
Parameters:
f - The float to formatdecimals - Number of decimal places to includeReturns: String with commas and limited decimal places
Example:
humanize.CommafWithDigits(834142.32, 1) // "834,142.3"
humanize.CommafWithDigits(834142.32, 0) // "834,142"
humanize.CommafWithDigits(1234.5678, 2) // "1,234.57"func BigComma(b *big.Int) stringAdds commas as thousand separators to a big.Int value.
Parameters:
b - The big.Int to formatReturns: String with commas every three digits
Example:
import "math/big"
num := big.NewInt(1234567890)
humanize.BigComma(num) // "1,234,567,890"func BigCommaf(v *big.Float) stringAdds commas as thousand separators to a big.Float value.
Parameters:
v - The big.Float to formatReturns: String with commas every three digits in the integer part
Example:
import "math/big"
num := big.NewFloat(1234567.89)
humanize.BigCommaf(num) // "1,234,567.89"func Ftoa(num float64) stringConverts a float to a string without trailing zeros.
Parameters:
num - The float to convertReturns: String representation without trailing zeros
Example:
humanize.Ftoa(2.24) // "2.24"
humanize.Ftoa(2.0) // "2"
humanize.Ftoa(2.100) // "2.1"
humanize.Ftoa(0.001) // "0.001"
// Compare with standard formatting:
fmt.Printf("%f", 2.24) // "2.240000"
humanize.Ftoa(2.24) // "2.24"func FtoaWithDigits(num float64, digits int) stringConverts a float to a string, limiting decimal places and removing trailing zeros.
Parameters:
num - The float to convertdigits - Maximum number of decimal placesReturns: String representation with limited decimals and no trailing zeros
Example:
humanize.FtoaWithDigits(2.24567, 2) // "2.25" (rounded)
humanize.FtoaWithDigits(2.10000, 3) // "2.1" (trailing zeros removed)
humanize.FtoaWithDigits(2.0, 2) // "2"func FormatFloat(format string, n float64) stringFormats a float with custom thousands and decimal separators based on a format string.
Parameters:
format - Format string defining separators and precision (see examples below)n - The float to formatReturns: Formatted string according to the format specification
Format String Examples:
The format string uses # for digits, and the first non-# character becomes the thousands separator, the second becomes the decimal separator.
// Standard US format
humanize.FormatFloat("#,###.##", 12345.6789) // "12,345.67"
// European format (space as thousands, comma as decimal)
humanize.FormatFloat("#\u202F###,##", 12345.6789) // "12 345,68"
// No decimal separator
humanize.FormatFloat("#,###.", 12345.6789) // "12,345"
humanize.FormatFloat("#,###", 12345.6789) // "12345,678"
// High precision
humanize.FormatFloat("#.###,######", 12345.6789) // "12.345,678900"
// Default format (empty string)
humanize.FormatFloat("", 12345.6789) // "12,345.67"Notes:
func FormatInteger(format string, n int) stringFormats an integer with custom thousands separator based on a format string. Convenient for template usage.
Parameters:
format - Format string defining the thousands separatorn - The integer to formatReturns: Formatted string according to the format specification
Example:
humanize.FormatInteger("#,###", 12345) // "12,345"
humanize.FormatInteger("#\u202F###", 12345) // "12 345"
humanize.FormatInteger("", 12345) // "12,345" (default)size := int64(1234567)
fmt.Printf("File size: %s bytes", humanize.Comma(size))
// Output: "File size: 1,234,567 bytes"amount := 1234567.89
fmt.Printf("Total: $%s", humanize.Commaf(amount))
// Output: "Total: $1,234,567.89"
// With controlled decimal places
fmt.Printf("Total: $%s", humanize.CommafWithDigits(amount, 2))
// Output: "Total: $1,234,567.89"percentage := 75.0
fmt.Printf("Progress: %s%%", humanize.Ftoa(percentage))
// Output: "Progress: 75%"
// Instead of:
fmt.Printf("Progress: %f%%", percentage)
// Output: "Progress: 75.000000%"// German format (period for thousands, comma for decimal)
humanize.FormatFloat("#.###,##", 1234567.89) // "1.234.567,89"
// French format (space for thousands, comma for decimal)
humanize.FormatFloat("# ###,##", 1234567.89) // "1 234 567,89"