decimal

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 11 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal is a decimal implementation provides 18 max effective numbers. Decimal is immutable after creation, passed by value. Do not change its fields.

func FromDecimal128

func FromDecimal128(low, high uint64) Decimal

FromDecimal128 convert IEEE 754 decimal128 to Decimal. Decimal128 has greater range than Decimal, FromDecimal128 expect the argument must in range of Decimal.

func FromFloat

func FromFloat(v float64, scale uint8) Decimal

FromFloat convert float to decimal

func FromInt

func FromInt(i int64) Decimal

FromInt create Decimal from 64bit integer, set to scale to zero.

func FromString

func FromString(s string) (Decimal, error)

FromString create decimal from string, scale set from fragment part of number. Such as '3.00', scale is 2.

func FromStringWithScale

func FromStringWithScale(str string, scale int) (Decimal, error)

FromStringWithScale create decimal from string, with specific scale. Use number's actual scale if it larger than specific scale. Examples:

FromStringWithScale("3", 2) // 3.00
FromStringWithScale("3.33", 0) // 3.33

func Zero

func Zero(scale int) Decimal

Zero returns zero decimal value with specific scale.

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

Add this value with other value, use two values' highest scale as result scale, such as 3.45 + 1 = 4.45.

func (Decimal) AddToScale

func (d Decimal) AddToScale(other Decimal, scale int) Decimal

AddToScale this value with other value round to specific scale.

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp the other value return -1 if < other, 1 if > other, 0 if equal. Cmp ignore scale, so 0.00 equals to 0

func (Decimal) Div

func (d Decimal) Div(other Decimal) Decimal

Div the other value, scale use max scale of current and other decimal.

func (Decimal) DivToScale

func (d Decimal) DivToScale(other Decimal, scale int) Decimal

DivToScale the other value and round result to specific scale.

func (Decimal) EQ

func (d Decimal) EQ(other Decimal) bool

EQ returns true if current value equals to other.

func (Decimal) Float64

func (d Decimal) Float64() float64

Float64 convert current value to float.

func (Decimal) GT

func (d Decimal) GT(other Decimal) bool

GT returns true if current value greater than other.

func (Decimal) GTE

func (d Decimal) GTE(other Decimal) bool

GTE returns true if current value greater or equal to other.

func (Decimal) GetBSON

func (d Decimal) GetBSON() (interface{}, error)

GetBSON implement bson.Getter interface, marshal value to mongoDB. Marshal to string to pressure both scale and value.

func (Decimal) GoString

func (d Decimal) GoString() string

GoString implement fmt.GoStringer interface. Adding 'm' suffix to result of String().

func (Decimal) Int64

func (d Decimal) Int64() int64

Int64 convert current value to int64, round tenth fragment.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if the value is 0, no matter what scale is.

func (Decimal) LT

func (d Decimal) LT(other Decimal) bool

LT returns true if current value less than other

func (Decimal) LTE

func (d Decimal) LTE(other Decimal) bool

LTE returns true if current value less or equal to other.

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) Decimal

Mul multiply the other value.

func (Decimal) MulToScale

func (d Decimal) MulToScale(other Decimal, scale int) Decimal

MulToScale multiply the other value and round to specific scale

func (Decimal) NE

func (d Decimal) NE(other Decimal) bool

NE returns true if current value not equals to other.

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns negative value

func (Decimal) Round

func (d Decimal) Round(scale int) Decimal

Round decimal to specific scale.

func (Decimal) Scale

func (d Decimal) Scale() uint8

Scale return scale of this decimal value.

func (*Decimal) SetBSON

func (d *Decimal) SetBSON(raw bson.Raw) error

SetBSON implement bson.Setter interface, marshal value from mongoDB.

func (Decimal) ShortString

func (d Decimal) ShortString() string

ShortString convert current value to string, removing ending 0s. Such as 3.00, returns 3.

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns 1 if current value greater than 0, -1 if less than 0, 0 if equals to 0.

func (Decimal) String

func (d Decimal) String() string

String implement fmt.Stringer interface, return decimal value in string format, appended 0 to scales. Such as 3.00, use ShortString() to get '3'.

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) Decimal

Sub subtract the other value.

func (Decimal) SubToScale

func (d Decimal) SubToScale(other Decimal, scale int) Decimal

SubToScale subtract the other value to specific scale.

func (Decimal) ToDecimal128

func (d Decimal) ToDecimal128() (low, high uint64)

ToDecimal128 convert to IEEE 754 decimal128

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(buf []byte) error

func (Decimal) Value

func (d Decimal) Value() (driver.Value, error)

Value implement database/sql/driver.Valuer interface. Return decimal value as string, actually an alias of String() method.

type NullDecimal

type NullDecimal struct {
	Decimal Decimal
	Valid   bool // Valid is true if current value is not NULL
}

NullDecimal nullable decimal value

func (NullDecimal) GetBSON

func (d NullDecimal) GetBSON() (interface{}, error)

GetBSON implement bson.Getter interface, marshal value to mongoDB. Marshal to string to pressure both scale and value.

func (NullDecimal) MarshalJSON

func (d NullDecimal) MarshalJSON() ([]byte, error)

func (*NullDecimal) SetBSON

func (d *NullDecimal) SetBSON(raw bson.Raw) error

SetBSON implement bson.Setter interface, marshal value from mongoDB

func (NullDecimal) String

func (d NullDecimal) String() string

func (*NullDecimal) UnmarshalJSON

func (d *NullDecimal) UnmarshalJSON(buf []byte) error

func (NullDecimal) Value

func (d NullDecimal) Value() (driver.Value, error)

Value implement driver.Valuer interface.

Jump to

Keyboard shortcuts

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