num

package
v1.0.27 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 6 Imported by: 23

Documentation

Index

Examples

Constants

View Source
const (
	KB = 1024
	MB = KB * KB
	GB = KB * MB
	TB = KB * GB // 1099511627776
	PB = KB * TB // 1125899906842624
	EB = KB * PB // 1152921504606846976
	ZB = KB * float64(EB)
	YB = KB * ZB
)

Variables

This section is empty.

Functions

func Abs added in v1.0.22

func Abs[T Signed](x T) T

Abs returns the absolute value of x.

func AbsFloat32 added in v1.0.22

func AbsFloat32(x float32) float32

AbsFloat32 returns the absolute value of x.

func AbsFloat64 added in v1.0.22

func AbsFloat64(x float64) float64

AbsFloat64 returns the absolute value of x.

func AbsInt added in v1.0.10

func AbsInt(x int) int

AbsInt returns the absolute value of x.

func AbsInt16 added in v1.0.22

func AbsInt16(x int16) int16

AbsInt16 returns the absolute value of x.

func AbsInt32 added in v1.0.10

func AbsInt32(x int32) int32

AbsInt32 returns the absolute value of x.

func AbsInt64 added in v1.0.10

func AbsInt64(x int64) int64

AbsInt64 returns the absolute value of x.

func AlphaToInt added in v1.0.27

func AlphaToInt(s string, defs ...int) int

AlphaToInt converts a string like "A", "Z", "AA" to a number (Excel-style).

func Atof

func Atof(s string, defs ...float64) float64

Atol use strconv.ParseFloat(s, 64) to parse string 's' to float64, return the first non-zero value of defs if error.

func Atoi

func Atoi(s string, defs ...int) int

Atoi use strconv.ParseInt(s, 0, strconv.IntSize) to parse string 's' to int, return the first non-zero value of defs if error.

func Atol

func Atol(s string, defs ...int64) int64

Atol use strconv.ParseInt(s, 0, 64) to parse string 's' to int64, return the first non-zero value of defs if error.

func Comma

func Comma(n any, args ...any) string

Comma produces a string form of the given number in base 10 with commas after every three orders of magnitude.

e.g. Comma(834142) -> 834,142 e.g. Comma(834142.1234, 3) -> 834,142.123 e.g. Comma(834142, "_") -> 834_142 e.g. Comma(834142.1234, "_", 3) -> 834_142.123

func CommaAny added in v1.0.27

func CommaAny(n any, args ...any) (string, error)

CommaAny produces a string form of the given number in base 10 with commas after every three orders of magnitude.

e.g. CommaAny(834142) -> 834,142 e.g. CommaAny(834142.1234, 3) -> 834,142.123 e.g. CommaAny(834142, "_") -> 834_142 e.g. CommaAny(834142.1234, "_", 3) -> 834_142.123

func CommaFloat

func CommaFloat(v float64, c string) string

CommaFloat produces a string form of the given number in base 10 with commas after every three orders of magnitude.

e.g. CommaFloat(834142.32) -> 834,142.32 e.g. CommaFloat(834142.32, "_") -> 834_142.32

func CommaFloatWithDigits

func CommaFloatWithDigits(f float64, digits int, c string) string

CommaFloatWithDigits works like the Commaf but limits the resulting string to the given number of decimal places.

e.g. CommaFloatWithDigits(834142.32, 1) -> 834,142.3 e.g. CommaFloatWithDigits(834142.32, 1, "_") -> 834_142.3

func CommaInt

func CommaInt(v int64, c string) string

CommaInt produces a string form of the given number in base 10 with commas after every three orders of magnitude.

e.g. CommaInt(834142) -> 834,142 e.g. CommaInt(834142, "_") -> 834_142

func CommaString added in v1.0.16

func CommaString(s string, c string) string

CommaString produces a string form of the given number string in base 10 with commas after every three orders of magnitude.

e.g. CommaString("834142.32") -> 834,142.32 e.g. CommaString("834142.32", "_") -> 834_142.32

func CommaUint

func CommaUint(v uint64, c string) string

CommaUint produces a string form of the given number in base 10 with commas after every three orders of magnitude.

e.g. CommaUint(834142) -> 834,142 e.g. CommaUint(834142, "_") -> 834_142

func CustomSize

func CustomSize(format string, size float64, base float64, units []string) string

CustomSize returns a human-readable approximation of a size using custom format.

func FormatAlpha added in v1.0.27

func FormatAlpha(n int) (string, error)

FormatAlpha converts a positive integer to Excel-style column string.

func FormatRoman added in v1.0.27

func FormatRoman(n int) (string, error)

FormatRoman converts a positive integer to roman number string.

func Ftoa

func Ftoa(f float64) string

Ftoa converts a float to a string with no trailing zeros.

func FtoaWithDigits

func FtoaWithDigits(f float64, digits int) string

FtoaWithDigits converts a float to a string but limits the resulting string to the given number of decimal places, and no trailing zeros.

func HumanSize

func HumanSize(n any, args ...any) string

HumanSize returns a human-readable approximation of a size with specified precision digit numbers (default: 2) (eg. "2.75 MB", "796 KB").

e.g. HumanSize(1234) -> 1.21 KB e.g. HumanSize(1234, 1) -> 1.2 KB e.g. HumanSize(1234, "") -> 1.21KB

Example
fmt.Println(HumanSize(1000))
fmt.Println(HumanSize(1024))
fmt.Println(HumanSize(1000000))
fmt.Println(HumanSize(1048576))
fmt.Println(HumanSize(2 * MB))
fmt.Println(HumanSize(3.42 * GB))
fmt.Println(HumanSize(5.372 * TB))
fmt.Println(HumanSize(2.22 * PB))

func HumanSizeAny added in v1.0.27

func HumanSizeAny(n any, args ...any) (string, error)

HumanSizeAny returns a human-readable approximation of a size with specified precision digit numbers (eg. "2.75 MB", "796 KB").

e.g. HumanSizeAny(1234) -> 1.21 KB e.g. HumanSizeAny(1234, 1) -> 1.2 KB e.g. HumanSizeAny(1234, "") -> 1.21KB

func HumanSizeFloat added in v1.0.27

func HumanSizeFloat(size float64, precision int, separator string) string

HumanSizeFloat returns a human-readable approximation of a size with specified precision digit numbers (eg. "2.75 MB", "796 KB").

func IfZero added in v1.0.22

func IfZero[T Number](a, b T) T

IfZero returns (a == 0 ? b : a)

func IntToAlpha added in v1.0.27

func IntToAlpha(n int) string

IntToAlpha converts a positive integer to Excel-style column string.

func IntToRoman added in v1.0.27

func IntToRoman(n int) string

IntToRoman converts a positive integer to roman number string.

func Itoa

func Itoa(n int) string

Itoa is equivalent to strconv.FormatInt(int64(n), 10).

func Ltoa

func Ltoa(n int64) string

Itoa is equivalent to strconv.FormatInt(n, 10).

func MustParseSize

func MustParseSize(size string) int64

MustParseSize returns an integer from a human-readable size using windows specification (KB = 1024B). panic if parse error

func MustParseSizeF

func MustParseSizeF(size string) float64

MustParseSizeF returns a float64 from a human-readable size using windows specification (KB = 1024B). panic if parse error

func NonZero added in v1.0.27

func NonZero[T Number](ns ...T) T

NonZero return the first non-zero value of defs if error.

func ParseAlpha added in v1.0.27

func ParseAlpha(s string) (int, error)

ParseAlpha converts a string like "A", "Z", "AA" to a number (Excel-style).

func ParseRoman added in v1.0.27

func ParseRoman(s string) (int, error)

ParseRoman converts a roman numeric string to integer.

func ParseSize

func ParseSize(size string) (int64, error)

ParseSize returns an integer from a human-readable size using windows specification (KB = 1024B).

Example
fmt.Println(ParseSize("32"))
fmt.Println(ParseSize("32b"))
fmt.Println(ParseSize("32B"))
fmt.Println(ParseSize("32k"))
fmt.Println(ParseSize("32K"))
fmt.Println(ParseSize("32kb"))
fmt.Println(ParseSize("32Kb"))
fmt.Println(ParseSize("32Mb"))
fmt.Println(ParseSize("32Gb"))
fmt.Println(ParseSize("32Tb"))
fmt.Println(ParseSize("32Pb"))

func ParseSizeF

func ParseSizeF(size string) (float64, error)

ParseSizeF returns a float64 from a human-readable size using windows specification (KB = 1024B).

func RomanToInt added in v1.0.27

func RomanToInt(s string, defs ...int) int

RomanToInt converts a roman numeric string to integer.

func StripTrailingZeros added in v1.0.16

func StripTrailingZeros(s string) string

Types

type Number added in v1.0.22

type Number interface {
	byte | int | int16 | int32 | int64 | uint | uint16 | uint32 | uint64 | float32 | float64
}

type Signed added in v1.0.22

type Signed interface {
	int | int16 | int32 | int64 | float32 | float64
}

Jump to

Keyboard shortcuts

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