or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

byte-sizes.mdenglish.mdindex.mdnumber-formatting.mdordinals.mdsi-notation.mdtime-formatting.md
tile.json

number-formatting.mddocs/

Number Formatting

Adds thousand separators (commas) to numbers, cleans up float representations by removing trailing zeros, and provides custom formatting with configurable separators.

Import

import "github.com/dustin/go-humanize"

Comma Formatting

Comma

func Comma(v int64) string

Adds commas as thousand separators to an int64 value.

Parameters:

  • v - The integer to format

Returns: 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"

Commaf

func Commaf(v float64) string

Adds commas as thousand separators to a float64 value, preserving decimal places.

Parameters:

  • v - The float to format

Returns: 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"

CommafWithDigits

func CommafWithDigits(f float64, decimals int) string

Adds commas to a float64 and limits the decimal places to the specified number.

Parameters:

  • f - The float to format
  • decimals - Number of decimal places to include

Returns: 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"

BigComma

func BigComma(b *big.Int) string

Adds commas as thousand separators to a big.Int value.

Parameters:

  • b - The big.Int to format

Returns: String with commas every three digits

Example:

import "math/big"

num := big.NewInt(1234567890)
humanize.BigComma(num)  // "1,234,567,890"

BigCommaf

func BigCommaf(v *big.Float) string

Adds commas as thousand separators to a big.Float value.

Parameters:

  • v - The big.Float to format

Returns: 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"

Float Formatting Without Trailing Zeros

Ftoa

func Ftoa(num float64) string

Converts a float to a string without trailing zeros.

Parameters:

  • num - The float to convert

Returns: 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"

FtoaWithDigits

func FtoaWithDigits(num float64, digits int) string

Converts a float to a string, limiting decimal places and removing trailing zeros.

Parameters:

  • num - The float to convert
  • digits - Maximum number of decimal places

Returns: 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"

Custom Number Formatting

FormatFloat

func FormatFloat(format string, n float64) string

Formats 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 format

Returns: 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:

  • Maximum precision is 9 digits after the decimal separator
  • The format string determines both separators and precision
  • An empty format string uses default US formatting

FormatInteger

func FormatInteger(format string, n int) string

Formats an integer with custom thousands separator based on a format string. Convenient for template usage.

Parameters:

  • format - Format string defining the thousands separator
  • n - The integer to format

Returns: 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)

Common Use Cases

Displaying File Sizes with Context

size := int64(1234567)
fmt.Printf("File size: %s bytes", humanize.Comma(size))
// Output: "File size: 1,234,567 bytes"

Financial Amounts

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"

Clean Float Display

percentage := 75.0
fmt.Printf("Progress: %s%%", humanize.Ftoa(percentage))
// Output: "Progress: 75%"

// Instead of:
fmt.Printf("Progress: %f%%", percentage)
// Output: "Progress: 75.000000%"

International Number Formatting

// 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"