decimal

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: ISC Imports: 3 Imported by: 0

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 cast 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

View Source
const One = Decimal(scaleFactor)

One constant, to make initializations easier.

View Source
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

func NewFromFloat(f float64) Decimal

NewFromFloat converts a float64 to Decimal. Only 3 digits of precision after the decimal point are preserved.

func NewFromInt

func NewFromInt(i int64) Decimal

NewFromInt converts a int64 to Decimal. Multiplies by 1000 to get into Decimal scale.

func NewFromString

func NewFromString(s string) (Decimal, error)

NewFromString returns a Decimal from a string representation. Throws an error if integer parsing fails.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value of the decimal

func (Decimal) Add

func (d Decimal) Add(d1 Decimal) Decimal

Add returns d + d1

func (Decimal) Cmp

func (d Decimal) Cmp(d1 Decimal) int

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) Div

func (d Decimal) Div(d1 Decimal) Decimal

Div returns d / d1

func (Decimal) Float64

func (d Decimal) Float64() (f float64, exact bool)

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) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if d == 0

func (Decimal) Mul

func (d Decimal) Mul(d1 Decimal) Decimal

Mul returns d * d1

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns -d

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns:

-1 if d < 0

0 if d == 0

+1 if d > 0

func (Decimal) StringFixedBank

func (d Decimal) StringFixedBank() string

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

func (d Decimal) StringRound() string

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

func (d Decimal) StringTruncate() string

StringTruncate returns the whole-number (Int) part of d.

Example:

NewFromFloat(5.44).StringTruncate() == "5"

func (Decimal) Sub

func (d Decimal) Sub(d1 Decimal) Decimal

Sub returns d - d1

Jump to

Keyboard shortcuts

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