Documentation
¶
Index ¶
- func Atoi(v interface{}, def ...int) (number int)
- func Ceil[V Number](value, max V) V
- func Clamp[V Number](value, min, max V) V
- func DecimalLen[V Decimal](v V) (length int)
- func Floor[V Number](value, min V) V
- func IntegerLen[V Integers](v V) (length int)
- func Round[V Decimal](x V) (rounded int)
- func RoundDown[V Decimal](value V) (rounded int)
- func RoundUp[V Decimal](value V) (rounded int)
- func ToFloat64(v interface{}, def ...float64) float64
- func ToInt(v interface{}, def ...int) int
- func ToInt64(v interface{}, def ...int64) int64
- func ToNumber[V Number](v interface{}, def ...V) (value V, ok bool)
- func ToUint(v interface{}, def ...uint) uint
- func ToUint64(v interface{}, def ...uint64) uint64
- type Complex
- type Decimal
- type Integer
- type Integers
- type Number
- type UnsignedInteger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Atoi ¶
Atoi is a wrapper around strconv.Atoi with the given value converted to a string first using fmt.Sprintf with a "%v" replacement
def is an optional default value if the strconv.Atoi call returns an error, only the first def value is ever used and if there are no def values provided, math.MaxInt is returned
func Ceil ¶
func Ceil[V Number](value, max V) V
Ceil returns the value if it is less-than-or-equal-to the maximum and returns the maximum otherwise
func Clamp ¶
func Clamp[V Number](value, min, max V) V
Clamp returns the value if it is greater-than-or-equal-to the minimum and less-than-or-equal-to the maximum arguments; if the value is less than the minimum, the minimum is returned; if the value is greater than the maximum, the maximum is returned
func DecimalLen ¶ added in v1.1.0
DecimalLen returns the number of digits in the generic Decimal given
func Floor ¶
func Floor[V Number](value, min V) V
Floor returns the value if it is greater-than-or-equal-to the minimum and returns the minimum otherwise
func IntegerLen ¶ added in v1.1.0
IntegerLen returns the number of digits in the generic Integers given
func ToFloat64 ¶
ToFloat64 is a convenience wrapper around ToNumber with the primary difference that the "counld not convert and no default" case returns math.MaxFloat64 instead of zero
func ToInt ¶
ToInt is a convenience wrapper around ToNumber with the primary difference that the "counld not convert and no default" case returns math.MaxInt instead of zero
func ToInt64 ¶
ToInt64 is a convenience wrapper around ToNumber with the primary difference that the "counld not convert and no default" case returns math.MaxInt64 instead of zero
func ToNumber ¶ added in v1.2.0
ToNumber is a generic function for detecting the arbitrary value (v) type and converting the value (by recasting or by strconv parsing) to another Number type, using reflection to determine the correct means of conversion
If the value given cannot be transformed into the requested Number type the "ok" return value will be false. In these cases, if value returned will be either the first def value given or zero
Examples:
i, ok := ToNumber[int](struct{string}{"nope"}, 10) // ok == false; i == int(10) i, ok := ToNumber[int](struct{string}{"nope"}) // ok == false; i == int(0) type Thing string v := Thing("10") i, ok := ToNumber[int](v) // ok == true; i == int(10)
Types ¶
type Complex ¶
type Complex interface { ~complex64 | ~complex128 }
type Integers ¶
type Integers interface { Integer | UnsignedInteger }
type Number ¶
type Number interface { UnsignedInteger | Integer | Decimal }