89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// number.go
|
|
package kern
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func IsInteger(v any) (ok bool) {
|
|
_, ok = v.(int64)
|
|
return ok
|
|
}
|
|
|
|
func IsNumber(v any) (ok bool) {
|
|
return IsFloat(v) || IsInteger(v)
|
|
}
|
|
|
|
func IsNumOrFract(v any) (ok bool) {
|
|
return IsFloat(v) || IsInteger(v) || IsFraction(v)
|
|
}
|
|
|
|
func IsNumberString(v any) (ok bool) {
|
|
return IsString(v) || IsNumber(v)
|
|
}
|
|
|
|
func NumAsFloat(v any) (f float64) {
|
|
var ok bool
|
|
if f, ok = v.(float64); !ok {
|
|
if fract, ok := v.(*FractionType); ok {
|
|
f = fract.ToFloat()
|
|
} else {
|
|
i, _ := v.(int64)
|
|
f = float64(i)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func AnyInteger(v any) (i int64, ok bool) {
|
|
ok = true
|
|
switch intval := v.(type) {
|
|
case int:
|
|
i = int64(intval)
|
|
case uint8:
|
|
i = int64(intval)
|
|
case uint16:
|
|
i = int64(intval)
|
|
case uint64:
|
|
i = int64(intval)
|
|
case uint32:
|
|
i = int64(intval)
|
|
case int8:
|
|
i = int64(intval)
|
|
case int16:
|
|
i = int64(intval)
|
|
case int32:
|
|
i = int64(intval)
|
|
case int64:
|
|
i = intval
|
|
default:
|
|
ok = false
|
|
}
|
|
return
|
|
}
|
|
|
|
func ToGoInt(value any, description string) (i int, err error) {
|
|
if valueInt64, ok := value.(int64); ok {
|
|
i = int(valueInt64)
|
|
} else if valueInt, ok := value.(int); ok {
|
|
i = valueInt
|
|
} else {
|
|
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
|
}
|
|
return
|
|
}
|
|
|
|
func ToGoInt64(value any, description string) (i int64, err error) {
|
|
if valueInt64, ok := value.(int64); ok {
|
|
i = valueInt64
|
|
} else if valueInt, ok := value.(int); ok {
|
|
i = int64(valueInt)
|
|
} else {
|
|
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
|
}
|
|
return
|
|
}
|