num

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: MIT Imports: 5 Imported by: 0

README

num - Number Utility Functions for Go

The num package provides a comprehensive set of utility functions for working with numbers in Go. It offers functions for basic math operations, formatting, statistical calculations, and more.

Installation

go get github.com/gflydev/utils/num

Usage

import "github.com/gflydev/utils/num"

Functions

Basic Math Operations
Clamp

Constrains a number between lower and upper bounds.

result := num.Clamp(5, 0, 10)
// result: 5 (within bounds)

result := num.Clamp(-5, 0, 10)
// result: 0 (below lower bound)

result := num.Clamp(15, 0, 10)
// result: 10 (above upper bound)

result := num.Clamp(5, 10, 0)
// result: 5 (lower > upper, bounds are swapped)
InRange

Checks if a number is between start and end (inclusive).

result := num.InRange(3, 2, 4)
// result: true (within range)

result := num.InRange(2, 2, 4)
// result: true (at lower bound)

result := num.InRange(4, 2, 4)
// result: true (at upper bound)

result := num.InRange(1, 2, 4)
// result: false (below range)

result := num.InRange(5, 2, 4)
// result: false (above range)

result := num.InRange(3, 4, 2)
// result: true (start > end, bounds are swapped)
Random

Returns a random integer between min and max (inclusive).

result := num.Random(1, 10)
// result: a random number between 1 and 10
Round

Rounds a number to the nearest integer or to the specified precision.

result := num.Round(4.7)
// result: 5 (rounded to nearest integer)

result := num.Round(4.3)
// result: 4 (rounded to nearest integer)

result := num.Round(4.5)
// result: 5 (rounded to nearest integer)

result := num.Round(-4.7)
// result: -5 (rounded to nearest integer)

result := num.Round(-4.3)
// result: -4 (rounded to nearest integer)

// With precision
result := num.Round(4.7, 1)
// result: 4.7 (rounded to 1 decimal place)

result := num.Round(4.75, 1)
// result: 4.8 (rounded to 1 decimal place)

result := num.Round(4.749, 2)
// result: 4.75 (rounded to 2 decimal places)
Floor

Rounds a number down to the nearest integer or to the specified precision.

Parameters:

  • n (float64): The number to round down
  • precision (int, optional): The number of decimal places to round to. If not provided, rounds down to the nearest integer.

Returns:

  • (float64): The rounded down number

Examples:

result := num.Floor(4.7)
// result: 4 (rounded down to nearest integer)

result := num.Floor(4.3)
// result: 4 (rounded down to nearest integer)

result := num.Floor(4.0)
// result: 4 (rounded down to nearest integer)

result := num.Floor(-4.7)
// result: -5 (rounded down to nearest integer)

result := num.Floor(-4.3)
// result: -5 (rounded down to nearest integer)

// With precision
result := num.Floor(4.78, 1)
// result: 4.7 (rounded down to 1 decimal place)

result := num.Floor(4.753, 2)
// result: 4.75 (rounded down to 2 decimal places)

result := num.Floor(-4.78, 1)
// result: -4.8 (rounded down to 1 decimal place)
Ceil

Rounds a number up to the nearest integer or to the specified precision.

Parameters:

  • n (float64): The number to round up
  • precision (int, optional): The number of decimal places to round to. If not provided, rounds up to the nearest integer.

Returns:

  • (float64): The rounded up number

Examples:

result := num.Ceil(4.7)
// result: 5 (rounded up to nearest integer)

result := num.Ceil(4.3)
// result: 5 (rounded up to nearest integer)

result := num.Ceil(4.0)
// result: 4 (rounded up to nearest integer)

result := num.Ceil(-4.7)
// result: -4 (rounded up to nearest integer)

result := num.Ceil(-4.3)
// result: -4 (rounded up to nearest integer)

// With precision
result := num.Ceil(4.78, 1)
// result: 4.8 (rounded up to 1 decimal place)

result := num.Ceil(4.753, 2)
// result: 4.76 (rounded up to 2 decimal places)

result := num.Ceil(-4.78, 1)
// result: -4.7 (rounded up to 1 decimal place)
Ceiling

Rounds a number up to the nearest integer or to the specified precision (alias for Ceil).

Parameters:

  • number (float64): The number to round up
  • precision (int, optional): The number of decimal places to round to. If not provided, rounds up to the nearest integer.

Returns:

  • (float64): The rounded up number

Examples:

result := num.Ceiling(4.3)
// result: 5 (rounded up to nearest integer)

result := num.Ceiling(4.7)
// result: 5 (rounded up to nearest integer)

result := num.Ceiling(-4.3)
// result: -4 (rounded up to nearest integer)

result := num.Ceiling(-4.7)
// result: -4 (rounded up to nearest integer)

result := num.Ceiling(4.357, 2)
// result: 4.36 (rounded up to 2 decimal places)

result := num.Ceiling(4.351, 2)
// result: 4.36 (rounded up to 2 decimal places)

result := num.Ceiling(-4.357, 2)
// result: -4.35 (rounded up to 2 decimal places)
Max

Returns the maximum value from a list of numbers.

Parameters:

  • numbers (variadic float64): A variadic list of float64 numbers

Returns:

  • (float64): The maximum value from the list, or 0 if the list is empty

Examples:

result := num.Max(1, 2, 3)
// result: 3

result := num.Max(3, 2, 1)
// result: 3

result := num.Max(-1, -2, -3)
// result: -1

result := num.Max()
// result: 0 (empty list)
Min

Returns the minimum value from a list of numbers.

Parameters:

  • numbers (variadic float64): A variadic list of float64 numbers

Returns:

  • (float64): The minimum value from the list, or 0 if the list is empty

Examples:

result := num.Min(1, 2, 3)
// result: 1

result := num.Min(3, 2, 1)
// result: 1

result := num.Min(-1, -2, -3)
// result: -3

result := num.Min(7.5, 3.2, 9.8)
// result: 3.2

result := num.Min()
// result: 0 (empty list)
Sum

Calculates the sum of all numbers in the provided list.

Parameters:

  • numbers (variadic float64): A variadic list of float64 numbers

Returns:

  • (float64): The sum of all numbers in the list, or 0 if the list is empty

Examples:

result := num.Sum(1, 2, 3)
// result: 6

result := num.Sum(-1, 5, 3)
// result: 7

result := num.Sum(-1, -2, -3)
// result: -6

result := num.Sum(7.5, 3.2, 9.8)
// result: 20.5

result := num.Sum()
// result: 0 (empty list)
Mean

Calculates the arithmetic mean (average) of a list of numbers.

Parameters:

  • numbers (variadic float64): A variadic list of float64 numbers

Returns:

  • (float64): The arithmetic mean of the numbers, or 0 if the list is empty

Examples:

result := num.Mean(1, 2, 3)
// result: 2

result := num.Mean(1, 3, 5, 7)
// result: 4

result := num.Mean(2, 4, 6, 8, 10)
// result: 6

result := num.Mean()
// result: 0 (empty list)
Abs

Returns the absolute value of a number.

Parameters:

  • n (float64): The number to get the absolute value of

Returns:

  • (float64): The absolute value of the number

Examples:

result := num.Abs(5)
// result: 5

result := num.Abs(-5)
// result: 5

result := num.Abs(-3.14)
// result: 3.14

result := num.Abs(0)
// result: 0
Pow

Returns the base raised to the exponent power.

Parameters:

  • base (float64): The base number
  • exponent (float64): The exponent to raise the base to

Returns:

  • (float64): The result of base^exponent

Examples:

result := num.Pow(2, 3)
// result: 8 (2^3)

result := num.Pow(3, 2)
// result: 9 (3^2)

result := num.Pow(2, 0)
// result: 1 (any number raised to 0 is 1)

result := num.Pow(0, 2)
// result: 0 (0 raised to any positive power is 0)

result := num.Pow(-2, 2)
// result: 4 (-2^2)

result := num.Pow(-2, 3)
// result: -8 (-2^3)

result := num.Pow(4, 0.5)
// result: 2.0 (square root of 4)
Sqrt

Returns the square root of a number.

Parameters:

  • n (float64): The number to calculate the square root of

Returns:

  • (float64): The square root of the number

Examples:

result := num.Sqrt(9)
// result: 3

result := num.Sqrt(4)
// result: 2

result := num.Sqrt(2)
// result: 1.4142135623730951

result := num.Sqrt(0)
// result: 0

result := num.Sqrt(-1)
// result: NaN (Not a Number)

result := num.Sqrt(25)
// result: 5
Collection Operations
MaxBy

Returns the element from a collection that produces the maximum value when passed through the iteratee function.

Parameters:

  • collection ([]T): A slice of any type T
  • iteratee (func(T) float64): A function that takes an element of type T and returns a float64

Returns:

  • (T): The element that produces the maximum value, or zero value of T if collection is empty

Examples:

// Find the number with the largest square
result := num.MaxBy([]int{1, 2, 3, 4, 5}, func(n int) float64 { return float64(n * n) })
// result: 5

// Find the person with the highest age
type person struct {
    name string
    age  int
}
people := []person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}}
result := num.MaxBy(people, func(p person) float64 { return float64(p.age) })
// result: person{"Bob", 30}

// Find the person with the longest name
result := num.MaxBy(people, func(p person) float64 { return float64(len(p.name)) })
// result: person{"Charlie", 20}

// Empty collection
result := num.MaxBy([]person{}, func(p person) float64 { return float64(p.age) })
// result: person{} (zero value)
MinBy

Returns the element from a collection that produces the minimum value when passed through the iteratee function.

Parameters:

  • collection ([]T): A slice of any type T
  • iteratee (func(T) float64): A function that takes an element of type T and returns a float64

Returns:

  • (T): The element that produces the minimum value, or zero value of T if collection is empty

Examples:

// Find the number with the smallest square
result := num.MinBy([]int{1, 2, 3, 4, 5}, func(n int) float64 { return float64(n * n) })
// result: 1

// Find the person with the lowest age
type person struct {
    name string
    age  int
}
people := []person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}}
result := num.MinBy(people, func(p person) float64 { return float64(p.age) })
// result: person{"Charlie", 20}

// Find the person with the shortest name
result := num.MinBy(people, func(p person) float64 { return float64(len(p.name)) })
// result: person{"Bob", 30}

// Empty collection
result := num.MinBy([]person{}, func(p person) float64 { return float64(p.age) })
// result: person{} (zero value)
SumBy

Calculates the sum of values in a collection after applying the iteratee function to each element.

Parameters:

  • collection ([]T): A slice of any type T
  • iteratee (func(T) float64): A function that takes an element of type T and returns a float64

Returns:

  • (float64): The sum of all values after applying the iteratee function, or 0 if the collection is empty

Examples:

// Sum of ages
type person struct {
    name string
    age  int
}
people := []person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}}
result := num.SumBy(people, func(p person) float64 { return float64(p.age) })
// result: 75

// Sum of name lengths
result := num.SumBy(people, func(p person) float64 { return float64(len(p.name)) })
// result: 15

// Sum of doubled numbers
result := num.SumBy([]int{1, 2, 3, 4, 5}, func(n int) float64 { return float64(n * 2) })
// result: 30

// Empty collection
result := num.SumBy([]person{}, func(p person) float64 { return float64(p.age) })
// result: 0
MeanBy

Calculates the arithmetic mean (average) of values in a collection after applying the iteratee function to each element.

Parameters:

  • collection ([]T): A slice of any type T
  • iteratee (func(T) float64): A function that takes an element of type T and returns a float64

Returns:

  • (float64): The arithmetic mean of all values after applying the iteratee function, or 0 if the collection is empty

Examples:

// Mean of ages
type person struct {
    name string
    age  int
}
people := []person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}}
result := num.MeanBy(people, func(p person) float64 { return float64(p.age) })
// result: 25

// Mean of name lengths
result := num.MeanBy(people, func(p person) float64 { return float64(len(p.name)) })
// result: 5

// Mean of doubled numbers
result := num.MeanBy([]int{1, 2, 3, 4, 5}, func(n int) float64 { return float64(n * 2) })
// result: 6

// Empty collection
result := num.MeanBy([]person{}, func(p person) float64 { return float64(p.age) })
// result: 0
Formatting Functions
Format

Formats a number with grouped thousands and specified decimal places.

Parameters:

  • number (float64): The number to format
  • decimals (int): The number of decimal places to include
  • decimalSeparator (string): The character to use as decimal separator
  • thousandsSeparator (string): The character to use as thousands separator

Returns:

  • (string): The formatted number as a string

Examples:

result := num.Format(1234.5678, 2, ".", ",")
// result: "1,234.57"

result := num.Format(1234567.89, 1, ",", " ")
// result: "1 234 567,9"

result := num.Format(1000000, 0, ".", ",")
// result: "1,000,000"

result := num.Format(-1234.5678, 2, ".", ",")
// result: "-1,234.57"

result := num.Format(0.5678, 3, ".", ",")
// result: "0.568"
FormatPercentage

Formats a number as a percentage with the specified number of decimal places.

Parameters:

  • number (float64): The number to format as a percentage (in decimal form, e.g., 0.5 for 50%)
  • decimals (int): The number of decimal places to include

Returns:

  • (string): The formatted percentage as a string with a % symbol

Examples:

result := num.FormatPercentage(0.156, 1)
// result: "15.6%"

result := num.FormatPercentage(0.5, 0)
// result: "50%"

result := num.FormatPercentage(1, 2)
// result: "100.00%"

result := num.FormatPercentage(0, 1)
// result: "0.0%"

result := num.FormatPercentage(-0.25, 0)
// result: "-25%"
Percent

Calculates what percentage one number is of another.

Parameters:

  • number (float64): The numerator (the part)
  • total (float64): The denominator (the whole)
  • decimals (int, optional): The number of decimal places to round the result to. If not provided, the result is not rounded.

Returns:

  • (float64): The percentage value (number/total * 100), or 0 if total is 0

Examples:

result := num.Percent(25, 100)
// result: 25.0 (25% of 100)

result := num.Percent(1, 3, 2)
// result: 33.33 (1/3 as a percentage, rounded to 2 decimal places)

result := num.Percent(1, 0)
// result: 0 (to avoid division by zero)

result := num.Percent(0, 100)
// result: 0

result := num.Percent(50, 200, 1)
// result: 25.0

result := num.Percent(200, 50)
// result: 400

result := num.Percent(-25, 100)
// result: -25
Abbreviate

Formats a number to a compact form with K, M, B, T suffixes.

result := num.Abbreviate(1000)
// result: "1K"

result := num.Abbreviate(489939)
// result: "490K"

result := num.Abbreviate(1230000, 2)
// result: "1.23M"

result := num.Abbreviate(1000000000)
// result: "1B"

result := num.Abbreviate(1500000000000, 1)
// result: "1.5T"

result := num.Abbreviate(999)
// result: "999"

result := num.Abbreviate(-1234567, 1)
// result: "-1.2M"
ForHumans

Converts a number to a human-readable string with the appropriate unit (thousand, million, billion, trillion).

result := num.ForHumans(1000)
// result: "1 thousand"

result := num.ForHumans(489939)
// result: "490 thousand"

result := num.ForHumans(1230000, 2)
// result: "1.23 million"

result := num.ForHumans(1000000000)
// result: "1 billion"

result := num.ForHumans(1500000000000, 1)
// result: "1.5 trillion"

result := num.ForHumans(-1234567, 1)
// result: "-1.2 million"
FileSize

Formats a byte size to a human-readable string with the appropriate unit (B, KB, MB, GB, TB, PB).

result := num.FileSize(1024)
// result: "1 KB"

result := num.FileSize(1024 * 1024)
// result: "1 MB"

result := num.FileSize(1024, 2)
// result: "1.00 KB"

result := num.FileSize(1500)
// result: "1 KB"

result := num.FileSize(1500, 2)
// result: "1.46 KB"

result := num.FileSize(1500000)
// result: "1 MB"

result := num.FileSize(1500000, 1)
// result: "1.4 MB"
Currency and Locale Functions
CurrencySymbol

Returns the symbol for the given currency code.

result := num.CurrencySymbol("USD")
// result: "$"

result := num.CurrencySymbol("EUR")
// result: "€"

result := num.CurrencySymbol("GBP")
// result: "£"

result := num.CurrencySymbol("JPY")
// result: "¥"

result := num.CurrencySymbol("XYZ")
// result: "XYZ" (unknown currency code returns the code itself)
GetLocaleInfo

Returns formatting information for the given locale.

result := num.GetLocaleInfo("en")
// result: LocaleInfo{DecimalSeparator: ".", ThousandsSeparator: ",", SymbolPosition: "prefix"}

result := num.GetLocaleInfo("de")
// result: LocaleInfo{DecimalSeparator: ",", ThousandsSeparator: ".", SymbolPosition: "suffix"}

result := num.GetLocaleInfo("fr")
// result: LocaleInfo{DecimalSeparator: ",", ThousandsSeparator: " ", SymbolPosition: "suffix"}

result := num.GetLocaleInfo("xx")
// result: LocaleInfo{DecimalSeparator: ".", ThousandsSeparator: ",", SymbolPosition: "prefix"} (defaults to English)
Currency

Formats a number as a currency with the specified currency code and locale.

result := num.Currency(1000)
// result: "$1,000.00"

result := num.Currency(1000, map[string]interface{}{"in": "EUR"})
// result: "€1,000.00"

result := num.Currency(1000, map[string]interface{}{"in": "EUR", "locale": "de"})
// result: "1.000,00 €"

result := num.Currency(1000, map[string]interface{}{"in": "EUR", "locale": "de", "precision": 0})
// result: "1.000 €"

result := num.Currency(1234.56, map[string]interface{}{"in": "GBP"})
// result: "£1,234.56"

result := num.Currency(-1234.56, map[string]interface{}{"in": "EUR", "locale": "de"})
// result: "-1.234,56 €"
Utility Functions
Ordinal

Converts a number to its ordinal representation.

result := num.Ordinal(1)
// result: "1st"

result := num.Ordinal(2)
// result: "2nd"

result := num.Ordinal(21)
// result: "21st"

result := num.Ordinal(3)
// result: "3rd"

result := num.Ordinal(4)
// result: "4th"

result := num.Ordinal(11)
// result: "11th"

result := num.Ordinal(12)
// result: "12th"

result := num.Ordinal(13)
// result: "13th"
Pairs

Splits a number into pairs of ranges based on a given chunk size.

result := num.Pairs(25, 10)
// result: [[0, 9], [10, 19], [20, 25]]

result := num.Pairs(25, 10, map[string]int{"offset": 0})
// result: [[0, 10], [10, 20], [20, 25]]

result := num.Pairs(10, 5)
// result: [[0, 4], [5, 9], [10, 10]]

result := num.Pairs(10, 5, map[string]int{"offset": 0})
// result: [[0, 5], [5, 10]]

License

This package is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package num provides utility functions for number manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abbreviate added in v1.1.0

func Abbreviate(number float64, precision ...int) string

Abbreviate formats a number to a compact form with K, M, B, T suffixes.

Parameters:

  • number: The number to format
  • precision: Optional. The number of decimal places to include. Default is 0.

Returns:

  • string: The formatted number as a string with appropriate suffix (K for thousands, M for millions, B for billions, T for trillions)

Examples:

Abbreviate(1000)             // Returns "1K"
Abbreviate(489939)           // Returns "490K"
Abbreviate(1230000, 2)       // Returns "1.23M"
Abbreviate(1000000000)       // Returns "1B"
Abbreviate(1500000000000, 1) // Returns "1.5T"
Abbreviate(-1234567, 2)   // Returns "-1.23M"

func Abs

func Abs(n float64) float64

Abs returns the absolute value of a number.

Parameters:

  • n: The number to get the absolute value of

Returns:

  • float64: The absolute value of the number

Examples:

Abs(-5)    // Returns 5.0
Abs(5)     // Returns 5.0
Abs(-3.14) // Returns 3.14
Abs(0)     // Returns 0.0

func Ceil

func Ceil(n float64, precision ...int) float64

Ceil rounds a number up to the nearest integer or to the specified precision.

Parameters:

  • n: The number to round up
  • precision: Optional. The number of decimal places to round to. If not provided, rounds up to the nearest integer.

Returns:

  • float64: The rounded up number

Examples:

Ceil(4.3)     // Returns 5.0 (rounded up to nearest integer)
Ceil(4.78, 1) // Returns 4.8 (rounded up to 1 decimal place)
Ceil(4.71, 1) // Returns 4.8 (rounded up to 1 decimal place)
Ceil(-4.7)    // Returns -4.0 (rounded up to nearest integer)

func Ceiling added in v1.1.0

func Ceiling(number float64, precision ...int) float64

Ceiling rounds a number up to the nearest integer or to the specified precision.

Parameters:

  • number: The number to round up
  • precision: Optional. The number of decimal places to round to. If not provided, rounds up to the nearest integer.

Returns:

  • float64: The rounded up number

Examples:

Ceiling(4.3)      // Returns 5.0 (rounded up to nearest integer)
Ceiling(4.357, 2) // Returns 4.36 (rounded up to 2 decimal places)
Ceiling(4.352, 2) // Returns 4.36 (rounded up to 2 decimal places)
Ceiling(-4.7)     // Returns -4.0 (rounded up to nearest integer)
Ceiling(-4.7, 1)  // Returns -4.7 (rounded up to 1 decimal place)

func Clamp

func Clamp(n, lower, upper float64) float64

Clamp constrains a number between lower and upper bounds.

Parameters:

  • n: The number to clamp
  • lower: The lower bound
  • upper: The upper bound

Returns:

  • float64: The clamped value

Examples:

Clamp(10, 0, 5) // Returns 5 (n is above upper bound)
Clamp(-3, 0, 5) // Returns 0 (n is below lower bound)
Clamp(3, 0, 5)  // Returns 3 (n is within bounds)
Clamp(5, 10, 0) // Returns 5 (lower > upper, bounds are swapped)

func Currency added in v1.1.0

func Currency(number float64, options ...map[string]interface{}) string

Currency formats a number as a currency with the specified currency code and locale.

Parameters:

  • number: The number to format as currency
  • in: Optional. The currency code (e.g., "USD", "EUR"). Default is "USD".
  • locale: Optional. The locale code (e.g., "en", "de"). Default is "en".
  • precision: Optional. The number of decimal places. Default is 2.

Returns:

  • string: The formatted currency string

Examples:

Number::currency(1000)                                // Returns "$1,000.00"
Number::currency(1000, in: "EUR")                     // Returns "€1,000.00"
Number::currency(1000, in: "EUR", locale: "de")       // Returns "1.000,00 €"
Number::currency(1000, in: "EUR", locale: "de", precision: 0) // Returns "1.000 €"

func CurrencySymbol added in v1.1.0

func CurrencySymbol(code string) string

CurrencySymbol returns the symbol for the given currency code.

Parameters:

  • code: The ISO 4217 currency code (e.g., "USD", "EUR", "GBP")

Returns:

  • string: The currency symbol

func FileSize added in v1.1.0

func FileSize(bytes float64, precision ...int) string

FileSize formats a byte size to a human-readable string with the appropriate unit (B, KB, MB, GB, TB, PB).

Parameters:

  • bytes: The size in bytes
  • precision: Optional. The number of decimal places to include. Default is 0.

Returns:

  • string: The formatted file size as a string with appropriate unit

Examples:

FileSize(1024)             // Returns "1 KB"
FileSize(1024 * 1024)      // Returns "1 MB"
FileSize(1024, 2)          // Returns "1.00 KB"
FileSize(1500)             // Returns "1 KB"
FileSize(1500, 2)          // Returns "1.46 KB"
FileSize(1500000)          // Returns "1 MB"
FileSize(1500000, 1)       // Returns "1.4 MB"

func Floor

func Floor(n float64, precision ...int) float64

Floor rounds a number down to the nearest integer or to the specified precision.

Parameters:

  • n: The number to round down
  • precision: Optional. The number of decimal places to round to. If not provided, rounds down to the nearest integer.

Returns:

  • float64: The rounded down number

Examples:

Floor(4.7)     // Returns 4.0 (rounded down to nearest integer)
Floor(4.78, 1) // Returns 4.7 (rounded down to 1 decimal place)
Floor(4.75, 1) // Returns 4.7 (rounded down to 1 decimal place)
Floor(-4.2)    // Returns -5.0 (rounded down to nearest integer)

func ForHumans added in v1.1.0

func ForHumans(number float64, precision ...int) string

ForHumans formats a number to a human-readable string with the appropriate unit (thousand, million, billion, etc.).

Parameters:

  • number: The number to format
  • precision: Optional. The number of decimal places to include. Default is 0.

Returns:

  • string: The formatted number as a string with appropriate unit

Examples:

ForHumans(1000)             // Returns "1 thousand"
ForHumans(489939)           // Returns "490 thousand"
ForHumans(1230000, 2)       // Returns "1.23 million"
ForHumans(1000000000)       // Returns "1 billion"
ForHumans(1500000000000, 1) // Returns "1.5 trillion"

func Format added in v1.1.0

func Format(number float64, decimals int, decimalSeparator, thousandsSeparator string) string

Format formats a number with grouped thousands and specified decimal places.

Parameters:

  • number: The number to format
  • decimals: The number of decimal places to include
  • decimalSeparator: The character to use as decimal separator
  • thousandsSeparator: The character to use as thousands separator

Returns:

  • string: The formatted number as a string

Examples:

Format(1234.5678, 2, ".", ",")    // Returns "1,234.57"
Format(1234567.89, 1, ",", " ")   // Returns "1 234 567,9"
Format(1000000, 0, ".", ",")      // Returns "1,000,000"
Format(-1234.56, 2, ".", ",")     // Returns "-1,234.56"
Format(0.5, 2, ".", ",")          // Returns "0.50"

func FormatPercentage added in v1.1.0

func FormatPercentage(number float64, decimals int) string

FormatPercentage formats a number as a percentage with the specified number of decimal places.

Parameters:

  • number: The number to format as a percentage (in decimal form, e.g., 0.5 for 50%)
  • decimals: The number of decimal places to include

Returns:

  • string: The formatted percentage as a string with a % symbol

Examples:

FormatPercentage(0.156, 1)  // Returns "15.6%"
FormatPercentage(0.5, 0)    // Returns "50%"
FormatPercentage(1.0, 2)    // Returns "100.00%"
FormatPercentage(0.0, 0)    // Returns "0%"
FormatPercentage(-0.25, 1)  // Returns "-25.0%"

func InRange

func InRange(n, start, end float64) bool

InRange checks if a number is between start and end (inclusive).

Parameters:

  • n: The number to check
  • start: The start of the range
  • end: The end of the range

Returns:

  • bool: true if the number is within the range, false otherwise

Examples:

InRange(3, 2, 4)  // Returns true (n is within range)
InRange(2, 2, 4)  // Returns true (n is at lower bound)
InRange(4, 2, 4)  // Returns true (n is at upper bound)
InRange(1, 2, 4)  // Returns false (n is below range)
InRange(5, 2, 4)  // Returns false (n is above range)
InRange(3, 4, 2)  // Returns true (start and end are automatically ordered)

func Max

func Max(numbers ...float64) float64

Max returns the maximum value from a list of numbers.

Parameters:

  • numbers: A variadic list of float64 numbers

Returns:

  • float64: The maximum value from the list, or 0 if the list is empty

Examples:

Max(1, 2, 3)       // Returns 3.0
Max(-1, -5, -3)    // Returns -1.0
Max(7.5, 3.2, 9.8) // Returns 9.8
Max()              // Returns 0.0 (empty list)

func MaxBy

func MaxBy[T any](collection []T, iteratee func(T) float64) T

MaxBy returns the element from a collection that produces the maximum value when passed through the iteratee function.

Parameters:

  • collection: A slice of any type T
  • iteratee: A function that takes an element of type T and returns a float64

Returns:

  • T: The element that produces the maximum value, or zero value of T if collection is empty

Examples:

// Find the number with the largest square
MaxBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * n) }) // Returns 3

// Find the person with the highest age
type Person struct { Name string; Age int }
people := []Person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 22}}
MaxBy(people, func(p Person) float64 { return float64(p.Age) }) // Returns Person{"Bob", 30}

func Mean

func Mean(numbers ...float64) float64

Mean calculates the arithmetic mean (average) of a list of numbers.

Parameters:

  • numbers: A variadic list of float64 numbers

Returns:

  • float64: The arithmetic mean of the numbers, or 0 if the list is empty

Examples:

Mean(1, 2, 3)       // Returns 2.0
Mean(2, 4, 6, 8)    // Returns 5.0
Mean(7.5, 3.2, 9.8) // Returns 6.833333333333333
Mean()              // Returns 0.0 (empty list)

func MeanBy

func MeanBy[T any](collection []T, iteratee func(T) float64) float64

MeanBy calculates the arithmetic mean (average) of values in a collection after applying the iteratee function to each element.

Parameters:

  • collection: A slice of any type T
  • iteratee: A function that takes an element of type T and returns a float64

Returns:

  • float64: The arithmetic mean of all values after applying the iteratee function, or 0 if the collection is empty

Examples:

// Mean of doubled values
MeanBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * 2) }) // Returns 4.0 (mean of 2,4,6)

// Mean of ages
type Person struct { Name string; Age int }
people := []Person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 22}}
MeanBy(people, func(p Person) float64 { return float64(p.Age) }) // Returns 25.666666666666668

func Min

func Min(numbers ...float64) float64

Min returns the minimum value from a list of numbers.

Parameters:

  • numbers: A variadic list of float64 numbers

Returns:

  • float64: The minimum value from the list, or 0 if the list is empty

Examples:

Min(1, 2, 3)       // Returns 1.0
Min(-1, -5, -3)    // Returns -5.0
Min(7.5, 3.2, 9.8) // Returns 3.2
Min()              // Returns 0.0 (empty list)

func MinBy

func MinBy[T any](collection []T, iteratee func(T) float64) T

MinBy returns the element from a collection that produces the minimum value when passed through the iteratee function.

Parameters:

  • collection: A slice of any type T
  • iteratee: A function that takes an element of type T and returns a float64

Returns:

  • T: The element that produces the minimum value, or zero value of T if collection is empty

Examples:

// Find the number with the smallest square
MinBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * n) }) // Returns 1

// Find the person with the lowest age
type Person struct { Name string; Age int }
people := []Person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 22}}
MinBy(people, func(p Person) float64 { return float64(p.Age) }) // Returns Person{"Charlie", 22}

func Ordinal added in v1.1.0

func Ordinal(number int) string

Ordinal converts a number to its ordinal representation.

Parameters:

  • number: The number to convert to an ordinal

Returns:

  • string: The ordinal representation of the number

Examples:

Ordinal(1)  // Returns "1st"
Ordinal(2)  // Returns "2nd"
Ordinal(3)  // Returns "3rd"
Ordinal(4)  // Returns "4th"
Ordinal(11) // Returns "11th"
Ordinal(21) // Returns "21st"
Ordinal(22) // Returns "22nd"
Ordinal(23) // Returns "23rd"

func Pairs added in v1.1.0

func Pairs(total, chunkSize int, options ...map[string]int) [][]int

Pairs splits a number into pairs of ranges based on a given chunk size.

Parameters:

  • total: The total number to split into pairs
  • chunkSize: The size of each chunk
  • options: Optional. A map containing additional options:
  • "offset": The offset to use for calculating the end of each range. Default is -1.

Returns:

  • [][]int: An array of pairs, where each pair is an array of two integers representing the start and end of a range

Examples:

Pairs(25, 10)                  // Returns [[0, 9], [10, 19], [20, 25]]
Pairs(25, 10, map[string]int{"offset": 0}) // Returns [[0, 10], [10, 20], [20, 25]]

func Percent added in v1.1.0

func Percent(number, total float64, decimals ...int) float64

Percent calculates what percentage one number is of another.

Parameters:

  • number: The numerator (the part)
  • total: The denominator (the whole)
  • decimals: Optional. The number of decimal places to round the result to. If not provided, the result is not rounded.

Returns:

  • float64: The percentage value (number/total * 100), or 0 if total is 0

Examples:

Percent(25, 100)    // Returns 25.0 (25% of 100)
Percent(1, 3, 2)    // Returns 33.33 (1/3 as a percentage, rounded to 2 decimal places)
Percent(1, 4)       // Returns 25.0 (1/4 as a percentage)
Percent(1, 0)       // Returns 0.0 (to avoid division by zero)
Percent(-10, 50, 1) // Returns -20.0 (negative percentage)

func Pow

func Pow(base, exponent float64) float64

Pow returns the base raised to the exponent power.

Parameters:

  • base: The base number
  • exponent: The exponent to raise the base to

Returns:

  • float64: The result of base^exponent

Examples:

Pow(2, 3)    // Returns 8.0 (2^3)
Pow(10, 2)   // Returns 100.0 (10^2)
Pow(5, 0)    // Returns 1.0 (any number raised to 0 is 1)
Pow(2, -1)   // Returns 0.5 (2^-1 = 1/2)
Pow(4, 0.5)  // Returns 2.0 (square root of 4)

func Random

func Random(min, max int) int

Random returns a random integer between min and max (inclusive).

Parameters:

  • min: The minimum value (inclusive)
  • max: The maximum value (inclusive)

Returns:

  • int: A random integer between min and max

Examples:

Random(1, 10)  // Returns a random number between 1 and 10 (inclusive)
Random(5, 5)   // Always returns 5 (min equals max)
Random(10, 1)  // Works the same as Random(1, 10) (min and max are automatically ordered)

func Round

func Round(n float64, precision ...int) float64

Round rounds a number to the nearest integer or to the specified precision.

Parameters:

  • n: The number to round
  • precision: Optional. The number of decimal places to round to. If not provided, rounds to the nearest integer.

Returns:

  • float64: The rounded number

Examples:

Round(4.7)     // Returns 5.0 (rounded to nearest integer)
Round(4.3)     // Returns 4.0 (rounded to nearest integer)
Round(4.5)     // Returns 5.0 (rounded to nearest integer)
Round(-4.7)    // Returns -5.0 (rounded to nearest integer)
Round(-4.3)    // Returns -4.0 (rounded to nearest integer)
Round(4.7, 1)  // Returns 4.7 (rounded to 1 decimal place)
Round(4.75, 1) // Returns 4.8 (rounded to 1 decimal place)
Round(4.749, 2) // Returns 4.75 (rounded to 2 decimal places)

func Sqrt

func Sqrt(n float64) float64

Sqrt returns the square root of a number.

Parameters:

  • n: The number to calculate the square root of

Returns:

  • float64: The square root of the number

Examples:

Sqrt(9)     // Returns 3.0
Sqrt(2)     // Returns 1.4142135623730951
Sqrt(0)     // Returns 0.0
Sqrt(-1)    // Returns NaN (Not a Number)
Sqrt(25)    // Returns 5.0

func Sum

func Sum(numbers ...float64) float64

Sum calculates the sum of all numbers in the provided list.

Parameters:

  • numbers: A variadic list of float64 numbers

Returns:

  • float64: The sum of all numbers in the list, or 0 if the list is empty

Examples:

Sum(1, 2, 3)       // Returns 6.0
Sum(-1, 5, 3)      // Returns 7.0
Sum(7.5, 3.2, 9.8) // Returns 20.5
Sum()              // Returns 0.0 (empty list)

func SumBy

func SumBy[T any](collection []T, iteratee func(T) float64) float64

SumBy calculates the sum of values in a collection after applying the iteratee function to each element.

Parameters:

  • collection: A slice of any type T
  • iteratee: A function that takes an element of type T and returns a float64

Returns:

  • float64: The sum of all values after applying the iteratee function, or 0 if the collection is empty

Examples:

// Sum of doubled values
SumBy([]int{1, 2, 3}, func(n int) float64 { return float64(n * 2) }) // Returns 12.0 (2+4+6)

// Sum of ages
type Person struct { Name string; Age int }
people := []Person{{"Alice", 25}, {"Bob", 30}, {"Charlie", 22}}
SumBy(people, func(p Person) float64 { return float64(p.Age) }) // Returns 77.0 (25+30+22)

Types

type LocaleInfo added in v1.1.0

type LocaleInfo struct {
	DecimalSeparator   string
	ThousandsSeparator string
	SymbolPosition     string // "prefix" or "suffix"
}

LocaleInfo contains formatting information for a specific locale.

func GetLocaleInfo added in v1.1.0

func GetLocaleInfo(locale string) LocaleInfo

GetLocaleInfo returns formatting information for the given locale.

Parameters:

  • locale: The locale code (e.g., "en", "de", "fr")

Returns:

  • LocaleInfo: The formatting information for the locale

Jump to

Keyboard shortcuts

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