Documentation
¶
Overview ¶
Package num provides utility functions for number manipulation.
Index ¶
- func Abbreviate(number float64, precision ...int) string
- func Abs(n float64) float64
- func Ceil(n float64, precision ...int) float64
- func Ceiling(number float64, precision ...int) float64
- func Clamp(n, lower, upper float64) float64
- func Currency(number float64, options ...map[string]interface{}) string
- func CurrencySymbol(code string) string
- func FileSize(bytes float64, precision ...int) string
- func Floor(n float64, precision ...int) float64
- func ForHumans(number float64, precision ...int) string
- func Format(number float64, decimals int, decimalSeparator, thousandsSeparator string) string
- func FormatPercentage(number float64, decimals int) string
- func InRange(n, start, end float64) bool
- func Max(numbers ...float64) float64
- func MaxBy[T any](collection []T, iteratee func(T) float64) T
- func Mean(numbers ...float64) float64
- func MeanBy[T any](collection []T, iteratee func(T) float64) float64
- func Min(numbers ...float64) float64
- func MinBy[T any](collection []T, iteratee func(T) float64) T
- func Ordinal(number int) string
- func Pairs(total, chunkSize int, options ...map[string]int) [][]int
- func Percent(number, total float64, decimals ...int) float64
- func Pow(base, exponent float64) float64
- func Random(min, max int) int
- func Round(n float64, precision ...int) float64
- func Sqrt(n float64) float64
- func Sum(numbers ...float64) float64
- func SumBy[T any](collection []T, iteratee func(T) float64) float64
- type LocaleInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abbreviate ¶ added in v1.1.0
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 ¶
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 ¶
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
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 ¶
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
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
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
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 ¶
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
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
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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