Documentation ¶
Overview ¶
decimal is a thin wrapper around github.com/cockroachdb/apd/v3.
- provides support for JsonUnmarshal from both strings and integers.
- a set of API that is similar to github.com/shopspring/decimal, where a new value is created instead of setting to an `*Decimal`.
- support cobra cli value.
Index ¶
- Variables
- func NewBigIntFromUint64(i uint64) *apd.BigInt
- func SetUint64ToBigInt(i uint64, d *apd.BigInt) *apd.BigInt
- type Decimal
- func (d *Decimal) Add(d1 *Decimal) *Decimal
- func (x *Decimal) BigInt() *big.Int
- func (d *Decimal) Clone() *Decimal
- func (x *Decimal) Copy(d *Decimal) *Decimal
- func (d *Decimal) Div(d1 *Decimal) *Decimal
- func (d *Decimal) Equal(d1 *Decimal) bool
- func (d *Decimal) GreaterThan(d1 *Decimal) bool
- func (d *Decimal) LessThan(d1 *Decimal) bool
- func (d *Decimal) Mul(d1 *Decimal) *Decimal
- func (d *Decimal) RoundDown(places int32) *Decimal
- func (d *Decimal) RoundUp(places int32) *Decimal
- func (d *Decimal) Set(s string) error
- func (d *Decimal) SetUint64(i uint64) *Decimal
- func (d Decimal) String() string
- func (d *Decimal) Sub(d1 *Decimal) *Decimal
- func (d *Decimal) TryAdd(d1 *Decimal) (*Decimal, error)
- func (x *Decimal) TryBigInt() (*big.Int, error)
- func (d *Decimal) TryDiv(d1 *Decimal) (*Decimal, error)
- func (d *Decimal) TryMul(d1 *Decimal) (*Decimal, error)
- func (d *Decimal) TrySub(d1 *Decimal) (*Decimal, error)
- func (x *Decimal) TryUint64() (uint64, error)
- func (d *Decimal) Type() string
- func (x *Decimal) Uint64() uint64
- func (d *Decimal) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
var Zero = Decimal{}
Zero of the decimal
Functions ¶
func NewBigIntFromUint64 ¶
func NewBigIntFromUint64(i uint64) *apd.BigInt
NewIntFromUint64 converts uint64 to an *apd.BigInt
func SetUint64ToBigInt ¶
func SetUint64ToBigInt(i uint64, d *apd.BigInt) *apd.BigInt
SetUint64ToBigInt set to an apd.BigInt uint64
Types ¶
type Decimal ¶
type Decimal struct {
apd.Decimal
}
Decimal is a type around github.com/cockroachdb/apd/v3's Decimal.
func NewFromInt ¶ added in v1.0.3
func NewFromString ¶
NewFromString is forwarded from `apd.NewFromString`.
func NewFromUint64 ¶
NewFromUint64 converts an uint64 to a *Decimal
func (*Decimal) BigInt ¶
BigInt gets the `big.Int` representation of the decimal. Panics if x is not an integer. Use `TryBigInt` to catch the error.
func (*Decimal) Div ¶
Div divides d by d1 (d/d1), panics if there is an error. Use TryDiv to get error
func (*Decimal) GreaterThan ¶
func (*Decimal) Mul ¶
Mul multiply d by d1, panics if there is an error. Use TryMul to get the error.
func (*Decimal) Sub ¶
Sub subtracts d1 from d, panics if there is an error. Use TrySub to get the error.
func (*Decimal) TryBigInt ¶
TryBigInt tries to convert the decimal into a `big.Int`. Returns an error if the number is not an integer
func (*Decimal) TryUint64 ¶
TryUint64 returns the uint64 representation of x, if x cannot be represented in an uint64, an error is returned.
func (*Decimal) Uint64 ¶
Uint64 returns the uint64 representation of x. Panic if x cannot be presented as uint64
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON Unmarshals a json string or number into a Decimal.
It first attempts to call the unmarshal method on `apd.Decimal`, which takes an integer, and `SetString` of `apd.Decimal` after failure