num

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 6 Imported by: 14

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 Atof

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

Atol use strconv.ParseFloat(s, 64) to parse string 's' to float64, return n[0] if error.

func Atoi

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

Atoi use strconv.Atoi(s) to parse string 's' to int, return n[0] if error.

func Atol

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

Atol use strconv.ParseInt(s, 10, 64) to parse string 's' to int64, return n[0] if error.

func Comma

func Comma(n any, c ...string) 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, "_") -> 834_142

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, decimals 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 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 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(size float64) string

HumanSize returns a human-readable approximation of a size capped at 4 valid numbers (eg. "2.746 MB", "796 KB").

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(float64(3.42 * GB)))
fmt.Println(HumanSize(float64(5.372 * TB)))
fmt.Println(HumanSize(float64(2.22 * PB)))
Output:

func HumanSizeWithPrecision

func HumanSizeWithPrecision(size float64, precision int) string

HumanSizeWithPrecision allows the size to be in any precision, instead of 4 digit precision used in HumanSize.

func Itoa

func Itoa(i int) string

func Ltoa

func Ltoa(i int64) string

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 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"))
Output:

func ParseSizeF

func ParseSizeF(size string) (float64, error)

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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