Documentation
¶
Overview ¶
Package udecimal provides a high-performance, high-precision (up to 19 digits after the decimal point) decimal arithmetic library. It includes functions for parsing and performing arithmetic operations such as addition, subtraction, multiplication, and division on decimal numbers. The package is designed to handle decimal numbers with a high degree of precision and efficiency, making it suitable for high-traffic financial applications where both precision and performance are critical.
Maximum and default precision is 19 digits after the decimal point. The default precision can be changed globally to any value between 1 and 19 to suit your use case and make sure that the precision is consistent across the entire application. See SetDefaultPrecision for more details.
Codec ¶
The udecimal package supports various encoding and decoding mechanisms to facilitate easy integration with different data storage and transmission systems.
- Marshal/UnmarshalJSON
- Marshal/UnmarshalBinary: gob, protobuf
- SQL: The Decimal type implements the sql.Scanner interface, enabling seamless integration with SQL databases.
For more details, see the documentation for each method.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("123.456")
b := udecimal.MustParse("-12.3456")
fmt.Println(a.Add(b))
fmt.Println(a.Sub(b))
fmt.Println(a.Mul(b))
fmt.Println(a.Div(b))
fmt.Println(a.Div(udecimal.MustParse("7")))
}
Output: 111.1104 135.8016 -1524.1383936 -10 <nil> 17.6365714285714285714 <nil>
Example (Errors) ¶
package main
import (
"errors"
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
_, err := udecimal.MustParse("1").Div(udecimal.Zero)
fmt.Println(errors.Is(err, udecimal.ErrDivideByZero))
_, err = udecimal.Parse("1e5")
fmt.Println(errors.Is(err, udecimal.ErrInvalidFormat))
}
Output: true true
Example (JsonStruct) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
type Order struct {
Price udecimal.Decimal `json:"price"`
Qty udecimal.Decimal `json:"qty"`
}
b, _ := json.Marshal(Order{Price: udecimal.MustParse("12.50"), Qty: udecimal.MustParse("3")})
fmt.Println(string(b))
// quoted and unquoted numbers both decode
var o Order
err := json.Unmarshal([]byte(`{"price": 12.5, "qty": "3"}`), &o)
fmt.Println(o.Price, o.Qty, err)
}
Output: {"price":"12.5","qty":"3"} 12.5 3 <nil>
Example (Money) ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
// splitting 10.05 between two people
share, _ := udecimal.MustParse("10.05").Div64(2)
fmt.Println(share)
fmt.Println(share.RoundHAZ(2))
fmt.Println(share.RoundBank(2))
}
Output: 5.025 5.03 5.02
Example (Overview) ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
price, err := udecimal.Parse("19.99")
if err != nil {
panic(err)
}
// three items and 4.99 for shipping
total := price.Mul(udecimal.MustParse("3")).Add(udecimal.MustParse("4.99"))
// split three ways
share, err := total.Div(udecimal.MustParse("3"))
if err != nil {
panic(err) // only when dividing by zero
}
fmt.Println(total)
fmt.Println(share)
fmt.Println(share.RoundHAZ(2))
}
Output: 64.96 21.6533333333333333333 21.65
Index ¶
- Variables
- func SetDefaultParseMode(mode ParseMode)
- func SetDefaultPrecision(prec uint8)
- type Decimal
- func Avg(a Decimal, b ...Decimal) Decimal
- func Max(a Decimal, b ...Decimal) Decimal
- func Min(a Decimal, b ...Decimal) Decimal
- func MustFromFloat64(f float64) Decimal
- func MustFromInt64(coef int64, prec uint8) Decimal
- func MustFromUint64(coef uint64, prec uint8) Decimal
- func MustParse(s string) Decimal
- func NewFromFloat64(f float64) (Decimal, error)
- func NewFromHiLo(neg bool, hi uint64, lo uint64, prec uint8) (Decimal, error)
- func NewFromInt64(coef int64, prec uint8) (Decimal, error)
- func NewFromUint64(coef uint64, prec uint8) (Decimal, error)
- func Parse(s string) (Decimal, error)
- func Parts(num Decimal) (whole, frac Decimal)
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(e Decimal) Decimal
- func (d Decimal) Add64(e uint64) Decimal
- func (d Decimal) AppendBinary(b []byte) ([]byte, error)
- func (d Decimal) AppendText(b []byte) ([]byte, error)
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(e Decimal) int
- func (d Decimal) CopySign(sign Decimal) Decimal
- func (d Decimal) Div(e Decimal) (Decimal, error)
- func (d Decimal) Div64(v uint64) (Decimal, error)
- func (d Decimal) EncodeValues(key string, v *url.Values) error
- func (d Decimal) Equal(e Decimal) bool
- func (d Decimal) Floor() Decimal
- func (d Decimal) GreaterThan(e Decimal) bool
- func (d Decimal) GreaterThanOrEqual(e Decimal) bool
- func (d Decimal) InexactFloat64() float64
- func (d Decimal) Int64() (int64, error)
- func (d Decimal) IsNeg() bool
- func (d Decimal) IsPos() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LessThan(e Decimal) bool
- func (d Decimal) LessThanOrEqual(e Decimal) bool
- func (d Decimal) MarshalBinary() ([]byte, error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Mod(e Decimal) (Decimal, error)
- func (d Decimal) Mul(e Decimal) Decimal
- func (d Decimal) Mul64(v uint64) Decimal
- func (d Decimal) MustDiv(e Decimal) Decimal
- func (d Decimal) MustMod(e Decimal) Decimal
- func (d Decimal) MustQuoRem(e Decimal) (Decimal, Decimal)
- func (d Decimal) Neg() Decimal
- func (d Decimal) PowInt(e int) Decimaldeprecated
- func (d Decimal) PowInt32(e int32) (Decimal, error)
- func (d Decimal) PowToIntPart(e Decimal) (Decimal, error)
- func (d Decimal) Prec() int
- func (d Decimal) PrecUint() uint8
- func (d Decimal) QuoRem(e Decimal) (Decimal, Decimal, error)
- func (d Decimal) RoundAwayFromZero(prec uint8) Decimal
- func (d Decimal) RoundBank(prec uint8) Decimal
- func (d Decimal) RoundHAZ(prec uint8) Decimal
- func (d Decimal) RoundHTZ(prec uint8) Decimal
- func (d *Decimal) Scan(src any) error
- func (d Decimal) Sign() int
- func (d Decimal) Sqrt() (Decimal, error)
- func (d Decimal) String() string
- func (d Decimal) StringFixed(prec uint8) string
- func (d Decimal) Sub(e Decimal) Decimal
- func (d Decimal) Sub64(e uint64) Decimal
- func (d Decimal) ToHiLo() (neg bool, hi uint64, lo uint64, prec uint8, ok bool)
- func (d Decimal) Trunc(prec uint8) Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(data []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type NullDecimal
- type ParseMode
Examples ¶
- Package
- Package (Errors)
- Package (JsonStruct)
- Package (Money)
- Package (Overview)
- Avg
- Decimal.Abs
- Decimal.Add
- Decimal.Add64
- Decimal.AppendBinary
- Decimal.AppendText
- Decimal.Ceil
- Decimal.Cmp
- Decimal.CopySign
- Decimal.Div
- Decimal.Div64
- Decimal.Equal
- Decimal.Floor
- Decimal.GreaterThan
- Decimal.GreaterThanOrEqual
- Decimal.InexactFloat64
- Decimal.Int64
- Decimal.IsNeg
- Decimal.IsPos
- Decimal.IsZero
- Decimal.LessThan
- Decimal.LessThanOrEqual
- Decimal.MarshalBinary
- Decimal.MarshalJSON
- Decimal.MarshalText
- Decimal.Mod
- Decimal.Mul
- Decimal.Mul64
- Decimal.MustDiv
- Decimal.MustMod
- Decimal.MustQuoRem
- Decimal.Neg
- Decimal.PowInt
- Decimal.PowInt32
- Decimal.PowToIntPart
- Decimal.Prec
- Decimal.PrecUint
- Decimal.QuoRem
- Decimal.RoundAwayFromZero
- Decimal.RoundBank
- Decimal.RoundHAZ
- Decimal.RoundHTZ
- Decimal.Scan
- Decimal.Sign
- Decimal.Sqrt
- Decimal.String
- Decimal.StringFixed
- Decimal.Sub
- Decimal.Sub64
- Decimal.ToHiLo
- Decimal.Trunc
- Decimal.UnmarshalBinary
- Decimal.UnmarshalJSON
- Decimal.UnmarshalText
- Decimal.Value
- Max
- Min
- MustFromFloat64
- MustFromInt64
- MustFromUint64
- MustParse
- NewFromFloat64
- NewFromHiLo
- NewFromInt64
- NewFromUint64
- NullDecimal.Scan
- NullDecimal.Value
- Parse
- Parts
- SetDefaultParseMode
- SetDefaultPrecision
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPrecOutOfRange is returned when the decimal precision is greater than the default precision // default precision can be configured using SetDefaultPrecision, and its value is up to 19 ErrPrecOutOfRange = fmt.Errorf("precision out of range: more decimal places than the default precision") // ErrEmptyString is returned when the input string is empty ErrEmptyString = fmt.Errorf("can't parse empty string") // ErrMaxStrLen is returned when the input string is longer than 200 characters. // A number that long is unrealistic in financial systems, which this library is mainly designed for. ErrMaxStrLen = fmt.Errorf("string input exceeds maximum length %d", maxStrLen) // ErrInvalidFormat is returned when the input string is not in the correct format // It doesn't support scientific notation, such as 1e-2, 1.23e4, etc. ErrInvalidFormat = fmt.Errorf("invalid format") // ErrDivideByZero is returned when dividing by zero ErrDivideByZero = fmt.Errorf("can't divide by zero") // ErrSqrtNegative is returned when calculating square root of negative number ErrSqrtNegative = fmt.Errorf("can't calculate square root of negative number") // ErrInvalidBinaryData is returned when unmarshalling invalid binary data // The binary data should follow the format as described in MarshalBinary, // and hold a value whose string form is at most 200 characters, the same limit as Parse. ErrInvalidBinaryData = fmt.Errorf("invalid binary data") // ErrBinaryTooLarge is returned by MarshalBinary and AppendBinary when the encoded value would be longer than // 255 bytes, the most its 1-byte length field can hold. This happens for coefficients of about 600 digits or more. ErrBinaryTooLarge = fmt.Errorf("value is too large for the binary format") // ErrZeroPowNegative is returned when raising zero to a negative power ErrZeroPowNegative = fmt.Errorf("can't raise zero to a negative power") // ErrExponentTooLarge is returned when the exponent is too large and becomes impractical. ErrExponentTooLarge = fmt.Errorf("exponent is too large. Must be less than or equal math.MaxInt32") // ErrIntPartOverflow is returned when the integer part of the decimal is too large to fit in int64 ErrIntPartOverflow = fmt.Errorf("integer part is too large to fit in int64") )
var ( Zero = Decimal{} One = MustFromInt64(1, 0) )
Functions ¶
func SetDefaultParseMode ¶ added in v1.8.0
func SetDefaultParseMode(mode ParseMode)
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Parse("0.12345678901234567890123"))
udecimal.SetDefaultParseMode(udecimal.ParseModeTrunc)
defer udecimal.SetDefaultParseMode(udecimal.ParseModeError)
fmt.Println(udecimal.Parse("0.12345678901234567890123"))
}
Output: 0 precision out of range: more decimal places than the default precision 0.1234567890123456789 <nil>
func SetDefaultPrecision ¶
func SetDefaultPrecision(prec uint8)
SetDefaultPrecision changes the default precision for decimal numbers in the package. Max precision is 19 and is also default.
This function is particularly useful when you want to have your precision of the deicmal smaller than 19 across the whole application. It should be called only once at the beginning of your application
Panics if the new precision is greater than 19 (maxPrec) or new precision is 0
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
udecimal.SetDefaultPrecision(10)
defer udecimal.SetDefaultPrecision(19)
a := udecimal.MustParse("1.23")
b := udecimal.MustParse("4.12475")
c, _ := a.Div(b)
fmt.Println(c)
}
Output: 0.2981998909
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal represents a fixed-point decimal number. The number is represented as a struct with three fields: coef, neg, and prec.
- coef: the coefficient of the decimal number
- neg: true if the number is negative
- prec: the number of digits after the decimal point (0 to 19)
Decimal numbers are immutable and can be used in arithmetic operations such as addition, subtraction, multiplication, and division.
func Avg ¶ added in v1.11.1
Avg returns the average of the decimals, i.e. their sum divided by the count. If the result has more than defaultPrec fraction digits, it will be truncated to defaultPrec digits, similar to Div.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Avg(udecimal.MustParse("1"), udecimal.MustParse("2"), udecimal.MustParse("4")))
}
Output: 2.3333333333333333333
func Max ¶ added in v1.7.0
Max returns the maximum decimal from the list of decimals.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Max(udecimal.MustParse("1.23"), udecimal.MustParse("4.12475")))
}
Output: 4.12475
func Min ¶ added in v1.7.0
Min returns the minimum decimal from the list of decimals.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Min(udecimal.MustParse("1.23"), udecimal.MustParse("4.12475")))
}
Output: 1.23
func MustFromFloat64 ¶
MustFromFloat64 similars to NewFromFloat64, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustFromFloat64(1.234))
// cautious: result will lose some precision when converting to decimal
fmt.Println(udecimal.MustFromFloat64(123456789.1234567890123456789))
}
Output: 1.234 123456789.12345679
func MustFromInt64 ¶
MustFromInt64 similars to NewFromInt64, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustFromInt64(123, 3))
fmt.Println(udecimal.MustFromInt64(-12345, 2))
}
Output: 0.123 -123.45
func MustFromUint64 ¶
MustFromUint64 similars to NewFromUint64, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustFromUint64(123, 3))
fmt.Println(udecimal.MustFromUint64(12345, 2))
}
Output: 0.123 123.45
func MustParse ¶
MustParse similars to Parse, but pacnis instead of returning error.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1234567890123456789.1234567890123456789"))
fmt.Println(udecimal.MustParse("-1234567890123456789.1234567890123456789"))
fmt.Println(udecimal.MustParse("-0.00007890123456789"))
}
Output: 1234567890123456789.1234567890123456789 -1234567890123456789.1234567890123456789 -0.00007890123456789
func NewFromFloat64 ¶
NewFromFloat64 returns a decimal from float64.
**NOTE**: you'll expect to lose some precision for this method due to FormatFloat. See: https://github.com/golang/go/issues/29491
This method is only suitable for small numbers with low precision. e.g. 1.0001, 0.0001, -123.456, -1000000.123456. You should avoid using this method if your input number has high precision.
Returns error when:
- f is NaN or Inf
- error when parsing float to string and then to decimal
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.NewFromFloat64(1.234))
// cautious: result will lose some precision when converting to decimal
fmt.Println(udecimal.NewFromFloat64(123456789.1234567890123456789))
}
Output: 1.234 <nil> 123456789.12345679 <nil>
func NewFromHiLo ¶
NewFromHiLo returns a decimal from 128-bit unsigned integer (hi,lo)
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.NewFromHiLo(false, 1, 1, 10))
fmt.Println(udecimal.NewFromHiLo(true, 0, 123456, 4))
}
Output: 1844674407.3709551617 <nil> -12.3456 <nil>
func NewFromInt64 ¶
NewFromInt64 returns a decimal which equals to coef / 10^prec and coef is an int64. Trailing zeros wll be removed and the prec will also be adjusted
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.NewFromInt64(123, 3))
fmt.Println(udecimal.NewFromInt64(12345, 20))
}
Output: 0.123 <nil> 0 precision out of range: more decimal places than the default precision
func NewFromUint64 ¶
NewFromUint64 returns a decimal which equals to coef / 10^prec and coef is an uint64 Trailing zeros wll be removed and the prec will also be adjusted
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.NewFromUint64(123, 3))
fmt.Println(udecimal.NewFromUint64(12345, 2))
fmt.Println(udecimal.NewFromUint64(12345, 20))
}
Output: 0.123 <nil> 123.45 <nil> 0 precision out of range: more decimal places than the default precision
func Parse ¶
Parse parses a number in string to a decimal. The string must be in the format of: [+-]d{1,19}[.d{1,19}]
Returns error if:
- empty/invalid string
- the number has more than 19 digits after the decimal point
- string length exceeds maxStrLen (which is 200 characters. See ErrMaxStrLen for more details)
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Parse("1234567890123456789.1234567890123456789"))
fmt.Println(udecimal.Parse("-1234567890123456789.1234567890123456789"))
fmt.Println(udecimal.Parse("-0.00007890123456789"))
// error cases
fmt.Println(udecimal.Parse("0.12345678901234567890123"))
fmt.Println(udecimal.Parse(""))
fmt.Println(udecimal.Parse("1.123.123"))
}
Output: 1234567890123456789.1234567890123456789 <nil> -1234567890123456789.1234567890123456789 <nil> -0.00007890123456789 <nil> 0 precision out of range: more decimal places than the default precision 0 can't parse empty string 0 invalid format: can't parse '1.123.123'
func Parts ¶ added in v1.11.1
Parts returns the integer part and the fraction part of d, where whole = Trunc(0) and frac = d - whole. frac keeps the sign of d, so whole + frac == d.
Examples:
Parts(123.456) = (123, 0.456) Parts(-123.456) = (-123, -0.456)
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.Parts(udecimal.MustParse("123.456")))
fmt.Println(udecimal.Parts(udecimal.MustParse("-123.456")))
}
Output: 123 0.456 -123 -0.456
func (Decimal) Abs ¶
Abs returns |d|
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("-123.45").Abs())
}
Output: 123.45
func (Decimal) Add ¶
Add returns d + e
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
b := udecimal.MustParse("4.12475")
c := a.Add(b)
fmt.Println(c)
}
Output: 5.35475
func (Decimal) Add64 ¶
Add64 returns d + e where e is a uint64
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
c := a.Add64(4)
fmt.Println(c)
}
Output: 5.23
func (Decimal) AppendBinary ¶ added in v1.8.0
AppendBinary implements encoding.BinaryAppender interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a, _ := udecimal.MustParse("1.23").AppendBinary(nil)
b, _ := udecimal.MustParse("-1.2345").AppendBinary(nil)
c, _ := udecimal.MustParse("1234567890123456789.1234567890123456789").AppendBinary(nil)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
Output: [0 2 11 0 0 0 0 0 0 0 123] [1 4 11 0 0 0 0 0 0 48 57] [0 19 19 9 73 176 246 240 2 51 19 211 181 5 249 181 241 129 21]
func (Decimal) AppendText ¶ added in v1.8.0
AppendText implements the encoding.TextAppender interface. The result will not be quoted like MarshalJSON.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a, _ := udecimal.MustParse("1.23").AppendText(nil)
b, _ := udecimal.MustParse("-1.2345").AppendText(nil)
c, _ := udecimal.MustParse("1234567890123456789.1234567890123456789").AppendText(nil)
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
}
Output: 1.23 -1.2345 1234567890123456789.1234567890123456789
func (Decimal) Ceil ¶
Ceil returns the smallest integer value greater than or equal to d.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Ceil())
}
Output: 2
func (Decimal) Cmp ¶
Cmp compares two decimals d,e and returns:
-1 if d < e 0 if d == e +1 if d > e
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Cmp(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").Cmp(udecimal.MustParse("1.23")))
fmt.Println(udecimal.MustParse("1.23").Cmp(udecimal.MustParse("0.12475")))
}
Output: -1 0 1
func (Decimal) CopySign ¶ added in v1.11.0
CopySign returns a value with the magnitude of d and the sign of sign.
Examples:
CopySign(10, 2) = 10 CopySign(10, -3) = -10 CopySign(-10, 2) = 10 CopySign(-10, -3) = -10
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("-10").CopySign(udecimal.MustParse("2")))
fmt.Println(udecimal.MustParse("10").CopySign(udecimal.MustParse("-3")))
}
Output: 10 -10
func (Decimal) Div ¶
Div returns d / e. If the result has more than defaultPrec fraction digits, it will be truncated to defaultPrec digits.
Returns divide by zero error when e is zero
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Div(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").Div(udecimal.MustParse("0")))
}
Output: 0.2981998909024789381 <nil> 0 can't divide by zero
func (Decimal) Div64 ¶
Div64 returns d / e where e is a uint64. If the result has more than defaultPrec fraction digits, it will be truncated to defaultPrec digits.
Returns divide by zero error when e is zero
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Div64(4))
fmt.Println(udecimal.MustParse("1.23").Div64(0))
}
Output: 0.3075 <nil> 0 can't divide by zero
func (Decimal) EncodeValues ¶ added in v1.9.0
EncodeValues implements the query.Encoder interface by the go-querystring package.
func (Decimal) Equal ¶ added in v1.2.0
Equal reports whether the two decimals d and e are equal.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.123").Equal(udecimal.MustParse("-1.123")))
fmt.Println(udecimal.MustParse("1.123").Equal(udecimal.MustParse("1.1230000")))
}
Output: false true
func (Decimal) Floor ¶
Floor returns the largest integer value less than or equal to d.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Floor())
fmt.Println(udecimal.MustParse("-1.23").Floor())
}
Output: 1 -2
func (Decimal) GreaterThan ¶ added in v1.7.0
GreaterThan reports whether d > e.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").GreaterThan(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").GreaterThan(udecimal.MustParse("1.23")))
fmt.Println(udecimal.MustParse("1.23").GreaterThan(udecimal.MustParse("0.12475")))
}
Output: false false true
func (Decimal) GreaterThanOrEqual ¶ added in v1.7.0
GreaterThanOrEqual reports whether d >= e.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").GreaterThanOrEqual(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").GreaterThanOrEqual(udecimal.MustParse("1.23")))
fmt.Println(udecimal.MustParse("1.23").GreaterThanOrEqual(udecimal.MustParse("0.12475")))
}
Output: false true true
func (Decimal) InexactFloat64 ¶
InexactFloat64 returns the float64 representation of the decimal. The result may not be 100% accurate due to the limitation of float64 (less decimal precision).
Caution: this method will not return the exact number if the decimal is too large.
e.g. 123456789012345678901234567890123456789.9999999999999999999 -> 123456789012345680000000000000000000000
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").InexactFloat64())
fmt.Println(udecimal.MustParse("123456789.123456789").InexactFloat64())
}
Output: 1.23 1.2345678912345679e+08
func (Decimal) Int64 ¶ added in v1.6.0
Int64 returns the integer part of the decimal. Return error if the decimal is too large to fit in int64.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Int64())
fmt.Println(udecimal.MustParse("1234567890123456789.1234567890123456789").Int64())
fmt.Println(udecimal.MustParse("-1234567890123456789.1234567890123456789").Int64())
fmt.Println(udecimal.MustParse("0.123").Int64())
fmt.Println(udecimal.MustParse("-0.123").Int64())
fmt.Println(udecimal.MustParse("12345678901234567890123456.123456").Int64())
}
Output: 1 <nil> 1234567890123456789 <nil> -1234567890123456789 <nil> 0 <nil> 0 <nil> 0 integer part is too large to fit in int64
func (Decimal) IsNeg ¶
IsNeg returns
true if d < 0 false if d >= 0
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").IsNeg())
fmt.Println(udecimal.MustParse("-1.23").IsNeg())
fmt.Println(udecimal.MustParse("0").IsNeg())
}
Output: false true false
func (Decimal) IsPos ¶
IsPos returns
true if d > 0 false if d <= 0
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").IsPos())
fmt.Println(udecimal.MustParse("-1.23").IsPos())
fmt.Println(udecimal.MustParse("0").IsPos())
}
Output: true false false
func (Decimal) IsZero ¶
IsZero returns
true if d == 0 false if d != 0
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").IsZero())
fmt.Println(udecimal.MustParse("0").IsZero())
}
Output: false true
func (Decimal) LessThan ¶ added in v1.7.0
LessThan reports whether d < e.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").LessThan(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").LessThan(udecimal.MustParse("1.23")))
fmt.Println(udecimal.MustParse("1.23").LessThan(udecimal.MustParse("0.12475")))
}
Output: true false false
func (Decimal) LessThanOrEqual ¶ added in v1.7.0
LessThanOrEqual reports whether d <= e.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").LessThanOrEqual(udecimal.MustParse("4.12475")))
fmt.Println(udecimal.MustParse("1.23").LessThanOrEqual(udecimal.MustParse("1.23")))
fmt.Println(udecimal.MustParse("1.23").LessThanOrEqual(udecimal.MustParse("0.12475")))
}
Output: true true false
func (Decimal) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler interface with custom binary format.
Binary format: [overflow + neg] [prec] [total bytes] [coef] example 1: -1.2345 1st byte: 0b0000_0001 (overflow = false, neg = true) 2nd byte: 0b0000_0100 (prec = 4) 3rd byte: 0b0000_1011 (total bytes = 11) 4th-11th bytes: 0x0000_0000_0000_3039 (coef = 12345, only stores the coef.lo part) example 2: 1234567890123456789.1234567890123456789 1st byte: 0b0000_0000 (overflow = false, neg = false) 2nd byte: 0b0001_0011 (prec = 19) 3rd byte: 0b0001_0011 (total bytes = 19) 4th-11th bytes: 0x0949_b0f6_f002_3313 (coef.hi) 12th-19th bytes: 0xd3b5_05f9_b5f1_8115 (coef.lo)
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").MarshalBinary())
fmt.Println(udecimal.MustParse("-1.2345").MarshalBinary())
fmt.Println(udecimal.MustParse("1234567890123456789.1234567890123456789").MarshalBinary())
}
Output: [0 2 11 0 0 0 0 0 0 0 123] <nil> [1 4 11 0 0 0 0 0 0 48 57] <nil> [0 19 19 9 73 176 246 240 2 51 19 211 181 5 249 181 241 129 21] <nil>
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a, _ := udecimal.MustParse("1.23").MarshalJSON()
b, _ := udecimal.MustParse("-1.2345").MarshalJSON()
c, _ := udecimal.MustParse("1234567890123456789.1234567890123456789").MarshalJSON()
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
}
Output: "1.23" "-1.2345" "1234567890123456789.1234567890123456789"
func (Decimal) MarshalText ¶ added in v1.2.0
MarshalText implements the encoding.TextMarshaler interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a, _ := udecimal.MustParse("1.23").MarshalText()
b, _ := udecimal.MustParse("-1.2345").MarshalText()
c, _ := udecimal.MustParse("1234567890123456789.1234567890123456789").MarshalText()
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
}
Output: 1.23 -1.2345 1234567890123456789.1234567890123456789
func (Decimal) Mod ¶ added in v1.6.0
Mod is similar to Decimal.QuoRem but only returns the remainder
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Mod(udecimal.MustParse("0.5")))
fmt.Println(udecimal.MustParse("1.23").Mod(udecimal.MustParse("0")))
}
Output: 0.23 <nil> 0 can't divide by zero
func (Decimal) Mul ¶
Mul returns d * e. The result will have at most defaultPrec digits after the decimal point.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
b := udecimal.MustParse("4.12475")
c := a.Mul(b)
fmt.Println(c)
}
Output: 5.0734425
func (Decimal) Mul64 ¶
Mul64 returns d * e where e is a uint64. The result will have at most defaultPrec digits after the decimal point.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
c := a.Mul64(4)
fmt.Println(c)
}
Output: 4.92
func (Decimal) MustDiv ¶ added in v1.10.0
MustDiv similar to Div, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").MustDiv(udecimal.MustParse("4.12475")))
}
Output: 0.2981998909024789381
func (Decimal) MustMod ¶ added in v1.10.0
MustMod similar to Mod, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").MustMod(udecimal.MustParse("0.5")))
}
Output: 0.23
func (Decimal) MustQuoRem ¶ added in v1.10.0
MustQuoRem similar to QuoRem, but panics instead of returning error
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").MustQuoRem(udecimal.MustParse("0.5")))
}
Output: 2 0.23
func (Decimal) Neg ¶
Neg returns -d
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Neg())
fmt.Println(udecimal.MustParse("-1.23").Neg())
}
Output: -1.23 1.23
func (Decimal) PowInt
deprecated
Deprecated: Use [PowInt32] instead for correct handling of 0^0 and negative exponents. This function treats 0 raised to any power as 0, which may not align with mathematical conventions but is practical in certain cases. See: https://github.com/quagmt/udecimal/issues/25.
PowInt raises the decimal d to the integer power e (d^e).
Special cases:
- 0^e = 0 for any integer e
- d^0 = 1 for any decimal d ≠ 0
Examples:
PowInt(0, 0) = 0 PowInt(0, 1) = 0 PowInt(0, -1) = 0 PowInt(2, 0) = 1 PowInt(2.5, 2) = 6.25 PowInt(2.5, -2) = 0.16
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").PowInt(2))
fmt.Println(udecimal.MustParse("1.23").PowInt(0))
fmt.Println(udecimal.MustParse("1.23").PowInt(-2))
}
Output: 1.5129 1 0.6609822195782933439
func (Decimal) PowInt32 ¶ added in v1.6.0
PowInt32 returns d raised to the power of e, where e is an int32.
Returns:
- The result of d raised to the power of e.
- An error if d is zero and e is a negative integer.
Special cases:
- 0^0 = 1
- 0^(any negative integer) results in ErrZeroPowNegative
Examples:
PowInt32(0, 0) = 1 PowInt32(2, 0) = 1 PowInt32(0, 1) = 0 PowInt32(0, -1) results in an error PowInt32(2.5, 2) = 6.25 PowInt32(2.5, -2) = 0.16
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").PowInt32(2))
fmt.Println(udecimal.MustParse("1.23").PowInt32(0))
fmt.Println(udecimal.MustParse("1.23").PowInt32(-2))
fmt.Println(udecimal.MustParse("0").PowInt32(0))
fmt.Println(udecimal.MustParse("0").PowInt32(-2))
}
Output: 1.5129 <nil> 1 <nil> 0.6609822195782933439 <nil> 1 <nil> 0 can't raise zero to a negative power
func (Decimal) PowToIntPart ¶ added in v1.6.0
PowToIntPart raises the decimal d to the power of integer part of e (d^int(e)). This is useful when the exponent is an integer but stored in Decimal.
Returns error if:
- d is zero and e is a negative integer.
- |int(e)| > math.MaxInt32 (because MaxInt32 is already ~2 billion, supporting more than that value is not practical and unnecessary).
Special cases:
- 0^0 = 1
- 0^(any negative integer) results in ErrZeroPowNegative
Examples:
PowToIntPart(0, 0) = 1 PowToIntPart(2, 0) = 1 PowToIntPart(0, 1) = 0 PowToIntPart(0, -1) results in an error PowToIntPart(2.5, 2.6) = 2.5^2 = 6.25 PowToIntPart(2.5, -2.123) = 2.5^(-2) = 0.16
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").PowToIntPart(udecimal.MustParse("2.123")))
fmt.Println(udecimal.MustParse("1.23").PowToIntPart(udecimal.MustParse("0.123")))
fmt.Println(udecimal.MustParse("1.23").PowToIntPart(udecimal.MustParse("-2.123")))
fmt.Println(udecimal.MustParse("0").PowToIntPart(udecimal.MustParse("0.123")))
fmt.Println(udecimal.MustParse("0").PowToIntPart(udecimal.MustParse("-2.123")))
}
Output: 1.5129 <nil> 1 <nil> 0.6609822195782933439 <nil> 1 <nil> 0 can't raise zero to a negative power
func (Decimal) Prec ¶
Prec returns decimal precision as an integer
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Prec())
}
Output: 2
func (Decimal) PrecUint ¶ added in v1.3.0
PrecUint returns decimal precision as uint8 Useful when you want to use the precision in other functions like Decimal.RoundBank or Decimal.Trunc because they accept uint8
Example:
u := MustParse("0.000001")
d := MustParse("123.4567891") // 123.4567891, prec = 7
d = d.Trunc(u.PrecUint()) // 123.456789
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23456").PrecUint())
}
Output: 5
func (Decimal) QuoRem ¶ added in v1.6.0
QuoRem returns q and r where - q = d / e and q is an integer - r = d - q * e (r < e and r has the same sign as d)
The implementation is similar to C's fmod function. Returns divide by zero error when e is zero
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").QuoRem(udecimal.MustParse("0.5")))
fmt.Println(udecimal.MustParse("1.23").QuoRem(udecimal.MustParse("0")))
}
Output: 2 0.23 <nil> 0 0 can't divide by zero
func (Decimal) RoundAwayFromZero ¶ added in v1.6.0
RoundAwayFromZero rounds the decimal to the specified prec using AWAY FROM ZERO method (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). If differs from HALF AWAY FROM ZERO in a way that the number is always rounded away from zero (or to infinity) no matter if is 0.5 or not. In other libraries or languages, this method is also known as ROUND_UP.
Examples:
RoundAwayFromZero(1.12, 1) = 1.2 RoundAwayFromZero(1.15, 1) = 1.2 RoundAwayFromZero(-1.12, 1) = -1.2 RoundAwayFromZero(-1.15, 1) = -1.2
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.12").RoundAwayFromZero(1))
fmt.Println(udecimal.MustParse("1.15").RoundAwayFromZero(1))
fmt.Println(udecimal.MustParse("-1.12").RoundAwayFromZero(1))
fmt.Println(udecimal.MustParse("-1.15").RoundAwayFromZero(1))
}
Output: 1.2 1.2 -1.2 -1.2
func (Decimal) RoundBank ¶
RoundBank uses half up to even (banker's rounding) to round the decimal to the specified prec.
Examples:
RoundBank(1.12345, 4) = 1.1234 RoundBank(1.12335, 4) = 1.1234 RoundBank(1.5, 0) = 2 RoundBank(-1.5, 0) = -2
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.12345").RoundBank(4))
fmt.Println(udecimal.MustParse("1.12335").RoundBank(4))
fmt.Println(udecimal.MustParse("1.5").RoundBank(0))
fmt.Println(udecimal.MustParse("-1.5").RoundBank(0))
}
Output: 1.1234 1.1234 2 -2
func (Decimal) RoundHAZ ¶
RoundHAZ rounds the decimal to the specified prec using HALF AWAY FROM ZERO method (https://en.wikipedia.org/wiki/Rounding#Rounding_half_away_from_zero).
Examples:
RoundHAZ(1.12345, 4) = 1.1235 RoundHAZ(1.12335, 4) = 1.1234 RoundHAZ(1.5, 0) = 2 RoundHAZ(-1.5, 0) = -2
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.12345").RoundHAZ(4))
fmt.Println(udecimal.MustParse("1.12335").RoundHAZ(4))
fmt.Println(udecimal.MustParse("1.5").RoundHAZ(0))
fmt.Println(udecimal.MustParse("-1.5").RoundHAZ(0))
}
Output: 1.1235 1.1234 2 -2
func (Decimal) RoundHTZ ¶
RoundHTZ rounds the decimal to the specified prec using HALF TOWARD ZERO method (https://en.wikipedia.org/wiki/Rounding#Rounding_half_toward_zero).
Examples:
RoundHTZ(1.12345, 4) = 1.1234 RoundHTZ(1.12335, 4) = 1.1233 RoundHTZ(1.5, 0) = 1 RoundHTZ(-1.5, 0) = -1
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.12345").RoundHTZ(4))
fmt.Println(udecimal.MustParse("1.12335").RoundHTZ(4))
fmt.Println(udecimal.MustParse("1.5").RoundHTZ(0))
fmt.Println(udecimal.MustParse("-1.5").RoundHTZ(0))
}
Output: 1.1234 1.1233 1 -1
func (*Decimal) Scan ¶
Scan implements sql.Scanner interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
var a udecimal.Decimal
_ = a.Scan("1.23")
fmt.Println(a)
}
Output: 1.23
func (Decimal) Sign ¶
Sign returns:
-1 if d < 0 0 if d == 0 +1 if d > 0
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Sign())
fmt.Println(udecimal.MustParse("-1.23").Sign())
fmt.Println(udecimal.MustParse("0").Sign())
}
Output: 1 -1 0
func (Decimal) Sqrt ¶
Sqrt returns the square root of d using Newton-Raphson method. (https://en.wikipedia.org/wiki/Newton%27s_method) The result will have at most defaultPrec digits after the decimal point. Returns error if d < 0
Examples:
Sqrt(4) = 2 Sqrt(2) = 1.4142135623730950488
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.21").Sqrt())
fmt.Println(udecimal.MustParse("0").Sqrt())
fmt.Println(udecimal.MustParse("-1.21").Sqrt())
}
Output: 1.1 <nil> 0 <nil> 0 can't calculate square root of negative number
func (Decimal) String ¶
String returns the string representation of the decimal. Trailing zeros will be removed.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").String())
fmt.Println(udecimal.MustParse("-1.230000").String())
}
Output: 1.23 -1.23
func (Decimal) StringFixed ¶
StringFixed returns the string representation of the decimal with fixed prec. Trailing zeros will not be removed. If the decimal is integer, the fractional part will be padded with zeros. If prec is smaller then d.prec, the number will stay the same as the original.
Example:
1.23.StringFixed(4) -> 1.2300 -1.23.StringFixed(4) -> -1.2300 5.StringFixed(2) -> 5.00 5.123.StringFixed(2) -> 5.123
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1").StringFixed(2))
fmt.Println(udecimal.MustParse("1.23").StringFixed(4))
fmt.Println(udecimal.MustParse("-1.230000").StringFixed(5))
}
Output: 1.00 1.2300 -1.23000
func (Decimal) Sub ¶
Sub returns d - e
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
b := udecimal.MustParse("4.12475")
c := a.Sub(b)
fmt.Println(c)
}
Output: -2.89475
func (Decimal) Sub64 ¶
Sub64 returns d - e where e is a uint64
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
a := udecimal.MustParse("1.23")
c := a.Sub64(4)
fmt.Println(c)
}
Output: -2.77
func (Decimal) ToHiLo ¶ added in v1.7.0
ToHiLo returns the internal representation of the decimal.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").ToHiLo())
fmt.Println(udecimal.MustParse("1234567890123456789.1234567890123456789").ToHiLo())
fmt.Println(udecimal.MustParse("-1234567890123456789.1234567890123456789").ToHiLo())
fmt.Println(udecimal.MustParse("0.123").ToHiLo())
fmt.Println(udecimal.MustParse("-0.123").ToHiLo())
fmt.Println(udecimal.MustParse("12345678901234567890123456.123456").ToHiLo())
fmt.Println(udecimal.MustParse("0").ToHiLo())
fmt.Println(udecimal.MustParse("18446744073709551615").ToHiLo())
fmt.Println(udecimal.MustParse("18446744073709551617").ToHiLo())
fmt.Println(udecimal.MustParse("184467440737095516.15").ToHiLo())
fmt.Println(udecimal.MustParse("184467440737095516.16").ToHiLo())
fmt.Println(udecimal.MustParse("184467440737095516160.1844674407370955161").ToHiLo())
}
Output: false 0 123 2 true false 669260594276348691 15255105882844922133 19 true true 669260594276348691 15255105882844922133 19 true false 0 123 3 true true 0 123 3 true false 669260594276 6432227781799973440 6 true false 0 0 0 true false 0 18446744073709551615 0 true false 1 1 0 true false 0 18446744073709551615 2 true false 1 0 2 true false 0 0 0 false
func (Decimal) Trunc ¶
Trunc returns d after truncating the decimal to the specified prec.
Examples:
Trunc(1.12345, 4) = 1.1234 Trunc(1.12335, 4) = 1.1233
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.MustParse("1.23").Trunc(1))
fmt.Println(udecimal.MustParse("-1.23").Trunc(5))
}
Output: 1.2 -1.23
func (*Decimal) UnmarshalBinary ¶
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
var a udecimal.Decimal
_ = a.UnmarshalBinary([]byte{0, 2, 11, 0, 0, 0, 0, 0, 0, 0, 123})
fmt.Println(a)
}
Output: 1.23
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
var a udecimal.Decimal
_ = a.UnmarshalJSON([]byte("1.23"))
fmt.Println(a)
}
Output: 1.23
func (*Decimal) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements the encoding.TextUnmarshaler interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
var a udecimal.Decimal
_ = a.UnmarshalText([]byte("1.23"))
fmt.Println(a)
}
Output: 1.23
type NullDecimal ¶
NullDecimal is a nullable Decimal.
func (*NullDecimal) Scan ¶
func (d *NullDecimal) Scan(src any) error
Scan implements sql.Scanner interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
var a, b udecimal.NullDecimal
_ = a.Scan("1.23")
_ = b.Scan(nil)
fmt.Println(a)
fmt.Println(b)
}
Output: {1.23 true} {0 false}
func (NullDecimal) Value ¶
func (d NullDecimal) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
Example ¶
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
fmt.Println(udecimal.NullDecimal{Decimal: udecimal.MustParse("1.2345"), Valid: true}.Value())
fmt.Println(udecimal.NullDecimal{}.Value())
}
Output: 1.2345 <nil> <nil> <nil>
type ParseMode ¶ added in v1.8.0
type ParseMode int
const ( // Default parse mode will return error if the string number // has more than [defaultPrec] decimal digits. ParseModeError ParseMode = iota // ParseModeTrunc will not return error if the string number // has more than [defaultPrec] decimal digits and truncate the exceeded digits instead. // Use this mode if the data source (e.g. database, external API, etc.) stores data // that has more than [defaultPrec] decimal digits already, allowing for some // precision loss (if acceptable). ParseModeTrunc )