Documentation ¶
Overview ¶
Package decimal implements fixed-point decimal with accuracy to 3 digits of precision after the decimal point.
int64 is the underlying data type for speed of computation. However, using an int64 casted to Decimal will not work, one of the "New" functions must be used to get accurate results.
The package multiplies every source value by 1000, and then does integer math from that point forward, maintaining all values at that scale over every operation.
Note: For use in ledger. Cannot handle values over approx 900 trillion.
Index ¶
- Constants
- type Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d1 Decimal) Decimal
- func (d Decimal) Cmp(d1 Decimal) int
- func (d Decimal) Div(d1 Decimal) Decimal
- func (d Decimal) Float64() (f float64, exact bool)
- func (d Decimal) IsZero() bool
- func (d Decimal) Mul(d1 Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) Sign() int
- func (d Decimal) StringFixedBank() string
- func (d Decimal) StringRound() string
- func (d Decimal) StringTruncate() string
- func (d Decimal) Sub(d1 Decimal) Decimal
Constants ¶
const One = scaleFactor
One constant, to make initializations easier.
const Zero = Decimal(0)
Zero constant, to make initializations easier.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decimal ¶
type Decimal int64
Decimal represents a fixed-point decimal.
func NewFromFloat ¶
NewFromFloat converts a float64 to Decimal. Only 3 digits of precision after the decimal point are preserved.
func NewFromInt ¶
NewFromInt converts a int64 to Decimal. Multiplies by 1000 to get into Decimal scale.
func NewFromString ¶
NewFromString returns a Decimal from a string representation. Throws an error if integer parsing fails.
func (Decimal) Cmp ¶
Cmp compares the numbers represented by d and d1 and returns:
-1 if d < d1 0 if d == d1 +1 if d > d1
func (Decimal) Float64 ¶
Float64 returns the float64 value for d, and exact is always set to false. The signature is this way to match big.Rat
func (Decimal) StringFixedBank ¶
StringFixedBank returns a banker rounded fixed-point string with 2 digits after the decimal point.
Example:
NewFromFloat(5.455).StringFixedBank() == "5.46" NewFromFloat(5.445).StringFixedBank() == "5.44"
func (Decimal) StringRound ¶
StringRound returns the nearest rounded whole-number (Int) part of d. Example:
NewFromFloat(5.5).StringRound() == "6" NewFromFloat(5.4).StringRound() == "5" NewFromFloat(-5.4).StringRound() == "5" NewFromFloat(-5.5).StringRound() == "6"
func (Decimal) StringTruncate ¶
StringTruncate returns the whole-number (Int) part of d.
Example:
NewFromFloat(5.44).StringTruncate() == "5"