Documentation ¶
Index ¶
- func DerefOr[T ~float32 | ~float64](ptr *T, defaultVal T) T
- func Format[T ~float32 | ~float64](f T, thousandsSep, decimalSep rune, precision int, padPrecision bool) string
- func Parse(str string) (float64, error)
- func ParseDetails(str string) (f float64, thousandsSep, decimalSep rune, decimals int, err error)
- func RoundToDecimals[T ~float32 | ~float64](f T, decimals int) T
- func Valid[T ~float32 | ~float64](f T) bool
- func ValidAndHasSign[T ~float32 | ~float64](f T, sign int) bool
- type FormatDef
- type Tolerant
- func (f *Tolerant) AsFloatPtr() *float64
- func (f Tolerant) IsInf() bool
- func (f Tolerant) IsNaN() bool
- func (f *Tolerant) UnmarshalJSON(j []byte) error
- func (f Tolerant) Valid() bool
- func (f Tolerant) ValidAndGreaterZero() bool
- func (f Tolerant) ValidAndHasSign(sign int) bool
- func (f Tolerant) ValidAndSmallerZero() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
func Format[T ~float32 | ~float64](f T, thousandsSep, decimalSep rune, precision int, padPrecision bool) string
Format a float similar to strconv.Format with the 'f' format option, but with decimalSep as decimal separator instead of a point and optional thousands grouping of the integer part. Valid values for decimalSep are '.' and ','. If thousandsSep is not zero, then the integer part of the number is grouped with thousandsSep between every group of 3 digits from right to left. Valid rune values for thousandsSep are 0, ',', '.', "'" and thousandsSep must be different from decimalSep. The precision argument controls the number of digits (excluding the exponent). The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly. If padPrecision is true and precision is greater zero, then the end of the fractional part will be padded with '0' characters to reach the length of precision. See: https://en.wikipedia.org/wiki/Decimal_separator
func Parse ¶
Parse a float string compatible with Format. If a separator was not detected, then zero will be returned for thousandsSep or decimalSep. See: https://en.wikipedia.org/wiki/Decimal_separator
func ParseDetails ¶
ParseDetails parses a float string compatible with Format and returns the detected integer thousands separator and decimal separator characters. If a separator was not detected, then zero will be returned for thousandsSep or decimalSep. See: https://en.wikipedia.org/wiki/Decimal_separator
func RoundToDecimals ¶
RoundToDecimals returns the float rounded to the passed number of decimal places.
Types ¶
type FormatDef ¶
type FormatDef struct { ThousandsSep rune `json:"thousandsSep,string,omitempty"` DecimalSep rune `json:"decimalSep,string"` Precision int `json:"precision"` PadPrecision bool `json:"padPrecision"` }
FormatDef holds the definition for a float format
func NewFormatDef ¶
func NewFormatDef() *FormatDef
NewFormatDef returns a format definition with a dot as decimal separator and no precision limit.
type Tolerant ¶
type Tolerant float64
Tolerant is a float64 underneath that is tolerant in parsing non standard JSON
func (*Tolerant) AsFloatPtr ¶
AsFloatPtr returns f as *float64
func (*Tolerant) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler and accepts numbers, strings, and null. JSON null and "" will set the amout to zero.
func (Tolerant) ValidAndGreaterZero ¶
ValidAndGreaterZero returns if the float is neither infinite nor NaN and greater than zero.
func (Tolerant) ValidAndHasSign ¶
ValidAndHasSign returns if a.Valid() and if it has the same sign than the passed int argument or any sign if 0 is passed.
func (Tolerant) ValidAndSmallerZero ¶
ValidAndSmallerZero returns if the float is neither infinite nor NaN and smaller than zero.