Documentation
¶
Overview ¶
Package float parses and formats floating point numbers with locale-dependent separators and rounds them to decimal places.
The package provides:
- Parse and ParseDetails: parse numbers like "1.234,56", "1,234.56", "1'234.56", or "1 234,56" and report the detected thousands and decimal separators
- Format: format a float with a thousands and a decimal separator, rounded to a precision
- FormatDef: a format definition for Format with a JSON form, and a FormatDef.Parse method implementing strfmt.Parser
- Tolerant: a float64 that decodes JSON numbers, strings, and null
- RoundToDecimals, Valid, ValidAndHasSign, and DerefOr: generic helpers for float32 and float64
A single '.' or ',' is always the decimal separator, so "1,234" parses as 1.234 and not as 1234. The apostrophe and the space are only thousands separators, so "1'234" parses as 1234 and "1'5" is an error.
RoundToDecimals and Format round half away from zero, and they round the shortest decimal representation of a float instead of its binary value, so 1.005 rounds to 1.01 and 2.5 to 3.
See the README.md file of the package for the complete API reference, the JSON forms, and the known pitfalls.
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 DerefOr ¶
DerefOr dereferences ptr or returns defaultVal if ptr is nil. Provides safe pointer dereferencing for float types.
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 is the number of fractional digits the value is rounded to half away from zero. Like RoundToDecimals it rounds the shortest decimal representation of f, which is the decimal literal that parses back to f, instead of its binary value, so 1.005 formats as "1.01" and 2.5 as "3" with precisions 2 and 0, where strconv.FormatFloat rounds the binary value 1.00499999999999989342 to "1.00" and the tie 2.5 to even. The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly. Unlike by strconv.FormatFloat, negative zero and negative values that round to zero are formatted without a minus sign, like "0.00". If padPrecision is true, the fractional part is kept at exactly precision digits, padding the end with '0' characters when needed. If padPrecision is false, trailing fractional zeros are trimmed to return the shortest representation of the value rounded to precision. See: https://en.wikipedia.org/wiki/Decimal_separator
func Parse ¶
Parse a float string compatible with Format. It is ParseDetails without the detected separators and decimals, see ParseDetails for the separator rules. 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.
One sign, '+' or '-', may be the first or the last character, but not both, so "+1-" and "-1-" are errors. A '-' right after the decimal separator means no cents, so "12,-" and "12.-" are 12.
A single '.' or ',' is the decimal separator, so "1,234" parses as 1.234. The apostrophe and the space are only ever thousands separators, so "1'234" and "1 234" parse as 1234, and one that can't separate thousands, like in "1'5", is an error. Thousands separators have to be followed by exactly 3 digits and the group in front of the first one must have 1 to 3 digits. The returned decimalSep is always zero, '.' or ','. See: https://en.wikipedia.org/wiki/Decimal_separator
func RoundToDecimals ¶
RoundToDecimals returns f rounded half away from zero to the passed number of decimal places. Negative decimals round to tens, hundreds, and so on.
It rounds the shortest decimal representation of f, which is the decimal literal that parses back to f, instead of its binary value, so RoundToDecimals(1.005, 2) is 1.01 and not the 1 of the binary value 1.00499999999999989342. Format rounds the same way.
NaN and ±Inf are returned unchanged, and so is a value that has no more decimal places than requested. Only a rounded value beyond the range of T, like math.MaxFloat64 rounded to a multiple of 10^307, is ±Inf.
Types ¶
type FormatDef ¶
type FormatDef struct {
ThousandsSep rune `json:"thousandsSep,omitempty"`
DecimalSep rune `json:"decimalSep"`
Precision int `json:"precision"`
PadPrecision bool `json:"padPrecision"`
}
FormatDef holds the definition for a float format.
FormatDef implements encoding/json.Marshaler and encoding/json.Unmarshaler representing the separators as JSON strings of one character like "," or as empty string for a zero separator, see FormatDef.UnmarshalJSON. The json tags of the fields name the keys of that representation, but the separators are not written as numbers like their rune type.
func NewFormatDef ¶
func NewFormatDef() *FormatDef
NewFormatDef returns a format definition with a dot as decimal separator and no precision limit.
func (*FormatDef) Format ¶
Format formats the float64 value f using the thousands separator, decimal separator, precision, and padding settings defined in ff. A zero DecimalSep, like that of the zero FormatDef, formats a point, so FormatDef{Precision: -1} formats 1234.5 as "1234.5", where the package function Format panics for a zero decimalSep.
func (FormatDef) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler writing the separators as JSON strings of one character like "," or as empty string for a zero separator, where a zero ThousandsSep is omitted.
func (*FormatDef) Parse ¶
Parse implements the strfmt.Parser interface. It parses str with the package function Parse and returns the value formatted with ff like FormatDef.Format, so a zero DecimalSep formats a point. The langHints are ignored.
func (*FormatDef) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler reading the separators from JSON strings of one character like "," or from an empty string for a zero separator. Strings of decimal digits like "44" for ',' are read as the code point of the separator, which is how FormatDef was written before it implemented encoding/json.Marshaler. Fields missing in the JSON object keep their value. Separators and a precision that Format panics on are an error, like an invalid rune, the same rune for both separators, which a missing field that keeps its value can give, or a precision below -1, but a zero DecimalSep is accepted like for the zero FormatDef. It formats a point, so it is an error together with a point as ThousandsSep.
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 amount 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.