float

package
v0.0.0-...-b9f8b57 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DerefOr

func DerefOr[T ~float32 | ~float64](ptr *T, defaultVal T) T

DerefOr dereferences ptr or returns defaultVal if ptr is nil

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

func Parse(str string) (float64, error)

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

func ParseDetails(str string) (f float64, thousandsSep, decimalSep rune, decimals int, err error)

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

func RoundToDecimals[T ~float32 | ~float64](f T, decimals int) T

RoundToDecimals returns the float rounded to the passed number of decimal places.

func Valid

func Valid[T ~float32 | ~float64](f T) bool

Valid returns if the passed float is neither infinite nor NaN

func ValidAndHasSign

func ValidAndHasSign[T ~float32 | ~float64](f T, sign int) bool

ValidAndHasSign returns true if Valid(f) and if it has the same sign than the passed non zero int argument. If 0 is passed as sign then the sign check always returns true.

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.

func (*FormatDef) Format

func (ff *FormatDef) Format(f float64) string

func (*FormatDef) Parse

func (ff *FormatDef) Parse(str string, langHints ...language.Code) (normalized string, err error)

Parse implements the Parser interface

type Tolerant

type Tolerant float64

Tolerant is a float64 underneath that is tolerant in parsing non standard JSON

func (*Tolerant) AsFloatPtr

func (f *Tolerant) AsFloatPtr() *float64

AsFloatPtr returns f as *float64

func (Tolerant) IsInf

func (f Tolerant) IsInf() bool

IsNaN returns if the float is positive or negative infinity

func (Tolerant) IsNaN

func (f Tolerant) IsNaN() bool

IsNaN returns if the float is not a number (NaN)

func (*Tolerant) UnmarshalJSON

func (f *Tolerant) UnmarshalJSON(j []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler and accepts numbers, strings, and null. JSON null and "" will set the amout to zero.

func (Tolerant) Valid

func (f Tolerant) Valid() bool

Valid returns if the float is neither infinite nor NaN

func (Tolerant) ValidAndGreaterZero

func (f Tolerant) ValidAndGreaterZero() bool

ValidAndGreaterZero returns if the float is neither infinite nor NaN and greater than zero.

func (Tolerant) ValidAndHasSign

func (f Tolerant) ValidAndHasSign(sign int) bool

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

func (f Tolerant) ValidAndSmallerZero() bool

ValidAndSmallerZero returns if the float is neither infinite nor NaN and smaller than zero.

Jump to

Keyboard shortcuts

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