Documentation
¶
Index ¶
- Variables
- func ValidateFloatRange(i float64) error
- func ValidateIntRange(i int64) error
- type Numeric
- func (n Numeric) Abs() Numeric
- func (n Numeric) Add(n2 Numeric) Numeric
- func (n Numeric) Cmp(n2 Numeric) int
- func (n Numeric) Div(n2 Numeric) Numeric
- func (n Numeric) DivRem(n2 Numeric) (Numeric, Numeric)
- func (n Numeric) Float64() float64
- func (n Numeric) Format(f fmt.State, verb rune)
- func (n Numeric) HasOverflow() bool
- func (n Numeric) HasUnderflow() bool
- func (n Numeric) Int() int64
- func (n Numeric) IsEqual(n2 Numeric) bool
- func (n Numeric) IsGreaterEqual(n2 Numeric) bool
- func (n Numeric) IsGreaterThan(n2 Numeric) bool
- func (n Numeric) IsLessThan(n2 Numeric) bool
- func (n Numeric) IsLessThanEqual(n2 Numeric) bool
- func (n Numeric) IsNaN() bool
- func (n *Numeric) IsUnderOverNaN() bool
- func (n Numeric) IsZero() bool
- func (n Numeric) MarshalJSON() ([]byte, error)
- func (n Numeric) MarshalText() ([]byte, error)
- func (n Numeric) Mul(n2 Numeric) Numeric
- func (n Numeric) Neg() Numeric
- func (n Numeric) Round(places int, mode RoundMode) Numeric
- func (n Numeric) Sign() int
- func (n Numeric) String() string
- func (n Numeric) Sub(n2 Numeric) Numeric
- func (n Numeric) Truncate(n2 Numeric) Numeric
- func (n *Numeric) UnmarshalJSON(data []byte) error
- func (n *Numeric) UnmarshalText(text []byte) error
- type RoundMode
Examples ¶
- FromFloat64
- FromInt
- FromString
- Numeric.Abs
- Numeric.Add
- Numeric.Cmp
- Numeric.Div
- Numeric.DivRem
- Numeric.HasOverflow
- Numeric.HasUnderflow
- Numeric.IsEqual
- Numeric.IsGreaterEqual
- Numeric.IsGreaterThan
- Numeric.IsLessThan
- Numeric.IsLessThanEqual
- Numeric.IsNaN
- Numeric.IsZero
- Numeric.MarshalJSON
- Numeric.MarshalText
- Numeric.Mul
- Numeric.Neg
- Numeric.Sign
- Numeric.Sub
- Numeric.UnmarshalJSON
- Numeric.UnmarshalText
Constants ¶
This section is empty.
Variables ¶
var ( // ErrParseFormatNumeric indicates a general numeric format error. ErrParseFormatNumeric = errors.New("invalid numeric format") // ErrMultipleUnderflowSymbols is returned when multiple '~' underflow markers are present in the input. ErrMultipleUnderflowSymbols = errors.New("multiple underflow symbols (~) not allowed") // ErrMultipleOverflowSymbols is returned when multiple '<' overflow markers are present in the input. ErrMultipleOverflowSymbols = errors.New("multiple overflow symbols (<) not allowed") // ErrMultipleMinusSigns is returned when multiple '-' signs are found in the prefix, indicating conflicting negative flags. ErrMultipleMinusSigns = errors.New("multiple sign symbols (-) not allowed") // ErrMultiplePlusSigns is returned when multiple '+' signs are found in the prefix, indicating conflicting positive flags. ErrMultiplePlusSigns = errors.New("multiple sign symbols (+) not allowed") // ErrInvalidDecimalPoint is returned when an input contains more than one decimal point or uses it incorrectly with an exponent. ErrInvalidDecimalPoint = errors.New("invalid decimal points") // ErrMultipleExponents is returned when more than one exponent ('e' or 'E') is encountered in the input string. ErrMultipleExponents = errors.New("multiple exponents") // ErrMultipleExponentSigns is returned when multiple '+' or '-' signs are used in the exponent portion of the input. ErrMultipleExponentSigns = errors.New("multiple exponent signs") // ErrNoExponentValue is returned when an exponent character is present but not followed by a valid numeric value. ErrNoExponentValue = errors.New("no exponent value") // ErrNoDigitsInInput is returned when no numeric digits are found in the input. ErrNoDigitsInInput = errors.New("no digits in input") // ErrInvalidCharacter is returned when an invalid character is encountered in the input string. ErrInvalidCharacter = errors.New("invalid character") ErrIntegerOutOfRange = errors.New("integer value out of range for Numeric representation") ErrFloatOutOfRange = errors.New("float value out of range for Numeric representation") )
var Zero = Numeric{} // Zero represents the numeric zero value.
Functions ¶
func ValidateFloatRange ¶
ValidateFloatRange checks if an int is within the valid range for Numeric.
func ValidateIntRange ¶
ValidateIntRange checks if an int is within the valid range for Numeric.
Types ¶
type Numeric ¶
type Numeric struct {
// contains filtered or unexported fields
}
Numeric represents a fixed-point arbitrary-precision decimal number.
func FromFloat64 ¶
FromFloat64 creates a Numeric from a float64. NOTE!!: Precision may be lost depending on internal representation.
Example ¶
n := FromFloat64(3.14159) fmt.Println(n.Round(3, RoundHalfUp).String())
Output: 3.142
func FromInt ¶
FromInt creates a Numeric from an int.
Example ¶
n := FromInt(42) fmt.Println(n.String())
Output: 42
func FromString ¶
FromString parses a string into a Numeric. Returns an error on invalid format.
Example ¶
n, err := FromString("123.456")
if err != nil {
panic(err)
}
fmt.Println(n.String())
Output: 123.456
func (Numeric) Abs ¶
Abs returns the absolute value of n.
Example ¶
n, _ := FromString("-3.14")
fmt.Println(n.Abs().String())
Output: 3.14
func (Numeric) Add ¶
Add returns the sum of n and n2.
Example ¶
x, _ := FromString("1.5")
y, _ := FromString("2.25")
fmt.Println(x.Add(y).String())
Output: 3.75
func (Numeric) Cmp ¶
Cmp compares n to n2 and returns: -1 if n < n2,
0 if n == n2, 1 if n > n2.
Example ¶
x, _ := FromString("2")
y, _ := FromString("3")
fmt.Println(x.Cmp(y)) // -1 if x < y
Output: -1
func (Numeric) Div ¶
Div returns the quotient of n divided by n2.
Example ¶
x, _ := FromString("7")
y, _ := FromString("2")
fmt.Println(x.Div(y).Round(3, RoundHalfUp).String())
Output: 3.5
func (Numeric) DivRem ¶
DivRem returns the integer quotient and remainder of n / n2.
Example ¶
x, _ := FromString("10")
y, _ := FromString("3")
q, r := x.DivRem(y)
fmt.Printf("q=%v r=%v\n", q.String(), r.String())
Output: q=3 r=1
func (Numeric) Float64 ¶
Float64 converts the Numeric to a float64. NOTE!!: Precision loss possible; not safe for financial calculations.
func (Numeric) Format ¶
Format implements the fmt.Formatter interface for the Numeric type.
It supports the following format verbs:
Verb | Description -----|------------------------------------------------------------- v | Default format using String(). With '#' flag: Numeric(value) f | Decimal format using Float64() (e.g., 123.45) e | Scientific notation with 'e' using Float64() (e.g., 1.23e+02) E | Scientific notation with 'E' using Float64() (e.g., 1.23E+02) g | Compact float format using Float64() G | Compact float format (upper case) using Float64() d | Integer format using Int() (e.g., 123) s | String format using String() q | Quoted string format using String() (e.g., "123.45") other| Unsupported verb; output will be: %!<verb>(Numeric=<value>)
The method respects width and precision flags defined by fmt.State. For example, %.2f will format to 2 decimal places.
Example usage:
n := NumericFromString("123.45")
fmt.Printf("%v\n", n) // 123.45
fmt.Printf("%#v\n", n) // Numeric(123.45)
fmt.Printf("%.2f\n", n) // 123.45
fmt.Printf("%d\n", n) // 123
fmt.Printf("%q\n", n) // "123.45"
fmt.Printf("%x\n", n) // %!x(Numeric=123.45)
func (Numeric) HasOverflow ¶
HasOverflow returns true if the number has overflowed.
Example ¶
n, _ := FromString("1999999999999999999.999999999999999999999999999999999999")
fmt.Println(n.HasOverflow())
Output: true
func (Numeric) HasUnderflow ¶
HasUnderflow returns true if the number has underflow.
Example ¶
n, _ := FromString("0.0000000000000000000000000000000000001")
fmt.Println(n.HasUnderflow())
Output: true
func (Numeric) Int ¶
Int converts the Numeric to an int, discarding any fractional part. NOTE!!: Overflows are masked to int range; no error is returned.
func (Numeric) IsEqual ¶
IsEqual returns true if n == n2, considering special flags.
Example ¶
x, _ := FromString("3.00")
y, _ := FromString("3")
fmt.Println(x.IsEqual(y))
Output: true
func (Numeric) IsGreaterEqual ¶
IsGreaterEqual returns true if n >= n2.
Example ¶
x, _ := FromString("5")
y, _ := FromString("5")
fmt.Println(x.IsGreaterEqual(y))
Output: true
func (Numeric) IsGreaterThan ¶
IsGreaterThan returns true if n > n2.
Example ¶
x, _ := FromString("5")
y, _ := FromString("2")
fmt.Println(x.IsGreaterThan(y))
Output: true
func (Numeric) IsLessThan ¶
IsLessThan returns true if n < n2.
Example ¶
x, _ := FromString("2.5")
y, _ := FromString("3")
fmt.Println(x.IsLessThan(y))
Output: true
func (Numeric) IsLessThanEqual ¶
IsLessThanEqual returns true if n <= n2.
Example ¶
x, _ := FromString("2.5")
y, _ := FromString("2.5")
fmt.Println(x.IsLessThanEqual(y))
Output: true
func (Numeric) IsNaN ¶
IsNaN returns true if the value is Not-a-Number.
Example ¶
n, _ := FromString("NaN")
fmt.Println(n.IsNaN())
Output: true
func (*Numeric) IsUnderOverNaN ¶
IsUnderOverNaN returns true if the number is NaN, has overflow, or underflow.
func (Numeric) IsZero ¶
IsZero returns true if the number is exactly zero.
Example ¶
n1, _ := FromString("0")
n2, _ := FromString("0.00000000000000000000000000000000001")
fmt.Println(n1.IsZero(), n2.IsZero())
Output: true false
func (Numeric) MarshalJSON ¶
MarshalJSON implements json.Marshaler. NaN is serialized as the string "NaN".
Example ¶
n, _ := FromString("42.42")
json, _ := n.MarshalJSON()
fmt.Println(string(json))
Output: "42.42"
func (Numeric) MarshalText ¶
MarshalText implements encoding.TextMarshaler for text formats.
Example ¶
n, _ := FromString("1.23")
text, _ := n.MarshalText()
fmt.Println(string(text))
Output: 1.23
func (Numeric) Mul ¶
Mul returns the product of n and n2.
Example ¶
x, _ := FromString("2")
y, _ := FromString("3.5")
fmt.Println(x.Mul(y).String())
Output: 7
func (Numeric) Neg ¶
Neg returns the negated value of n.
Example ¶
n, _ := FromString("42")
fmt.Println(n.Neg().String())
Output: -42
func (Numeric) Round ¶
Round returns a new Numeric rounded to the specified number of decimal places. 'places' is digits after the decimal point. 0 means integer rounding. Underflow is removed.
func (Numeric) Sign ¶
Sign returns -1 if negative, 1 if zero or positive, 0 for NaN
Example ¶
n1, _ := FromString("-1")
n2, _ := FromString("0")
n3, _ := FromString("3.14")
fmt.Println(n1.Sign(), n2.Sign(), n3.Sign())
Output: -1 1 1
func (Numeric) String ¶
String returns the decimal string representation of the number. This function allocates to the heap the return string.
func (Numeric) Sub ¶
Sub returns the result of subtracting n2 from n.
Example ¶
x, _ := FromString("5")
y, _ := FromString("3.2")
fmt.Println(x.Sub(y).String())
Output: 1.8
func (*Numeric) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler for Numeric. Parses quoted decimal strings. Returns error on invalid input.
Example ¶
var n Numeric
err := n.UnmarshalJSON([]byte(`"789.01"`))
if err != nil {
panic(err)
}
fmt.Println(n.String())
Output: 789.01