numberformat

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package numberformat is a standard-library-only Go port of the number presentation helpers popularised by the npm packages numeral.js and accounting.js, which Express/Node apps use to render numbers for humans: thousands grouping, fixed-decimal formatting, currency and percentage formatting, ordinal suffixes and compact "1.2k / 3.4M" abbreviation.

The workhorse is FormatFloat, which rounds to a fixed number of decimal places (half away from zero), groups the integer part with a configurable thousands separator and joins it to the fraction with a configurable decimal separator. Comma and CommaFloat are the common English-locale shortcuts (comma grouping, dot decimal). FormatCurrency prefixes a symbol and FormatPercent multiplies by 100 and appends "%". Ordinal returns the English ordinal string for an integer ("1st", "22nd", "113th"), and Abbreviate renders large magnitudes compactly with k/M/B/T suffixes. RoundTo exposes the underlying decimal rounding.

Negative numbers keep their sign ahead of any grouping, rounding uses half-away-from-zero so 2.5 at zero decimals becomes 3, and all functions are deterministic and depend only on math, strconv and strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abbreviate

func Abbreviate(f float64, decimals int) string

Abbreviate renders f compactly with a magnitude suffix (k, M, B, T) and the given number of decimal places, e.g. Abbreviate(1500, 1) is "1.5k" and Abbreviate(2_300_000, 2) is "2.30M". Values below 1000 are formatted plainly.

func Comma

func Comma(n int64) string

Comma formats an integer with comma thousands grouping (English locale).

func CommaFloat

func CommaFloat(f float64, decimals int) string

CommaFloat formats a float with comma thousands grouping, a dot decimal separator and the given number of decimal places.

func FormatCurrency

func FormatCurrency(f float64, symbol string, decimals int) string

FormatCurrency formats f as a currency amount, prefixing symbol to the comma-grouped, fixed-decimal number. A negative amount places the sign before the symbol: FormatCurrency(-1234.5, "$", 2) is "-$1,234.50".

func FormatFloat

func FormatFloat(f float64, decimals int, decSep, thousandsSep string) string

FormatFloat formats f with exactly decimals fractional digits, grouping the integer part with thousandsSep and separating the fraction with decSep. The value is rounded half away from zero.

func FormatInt

func FormatInt(n int64, thousandsSep string) string

FormatInt formats an integer with the given thousands separator, e.g. FormatInt(1234567, ",") is "1,234,567".

func FormatPercent

func FormatPercent(f float64, decimals int) string

FormatPercent formats f as a percentage by multiplying by 100 and appending "%", with the given number of decimal places: FormatPercent(0.1234, 2) is "12.34%".

func Ordinal

func Ordinal(n int) string

Ordinal returns the English ordinal representation of n, e.g. 1 -> "1st", 2 -> "2nd", 3 -> "3rd", 11 -> "11th", 22 -> "22nd".

func RoundTo

func RoundTo(f float64, decimals int) float64

RoundTo rounds f to the given number of decimal places using half-away-from-zero rounding. A negative decimals value rounds to the left of the decimal point (RoundTo(1234, -2) is 1200).

func Unformat

func Unformat(s string) (float64, error)

Unformat parses a human-formatted number string back to a float64, ignoring any grouping separators, currency symbols, percent signs and surrounding text and keeping only digits, a single leading sign and the last decimal point. It mirrors accounting.js's unformat, so Unformat("$1,234.56") returns 1234.56. It returns an error when no numeric content is found.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL