decimal

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 4 Imported by: 2

README

decimal

Wrapper around cockroaches/apd/v3

Documentation

Go Reference

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

Constants

This section is empty.

Variables

View Source
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 New added in v1.0.3

func New(coeff int64, exponent int32) *Decimal

func NewFromInt added in v1.0.3

func NewFromInt(coeff int64) *Decimal

func NewFromString

func NewFromString(s string) (*Decimal, error)

NewFromString is forwarded from `apd.NewFromString`.

func NewFromUint64

func NewFromUint64(i uint64) *Decimal

NewFromUint64 converts an uint64 to a *Decimal

func (*Decimal) Add

func (d *Decimal) Add(d1 *Decimal) *Decimal

Add adds d and d1, panics if there is an error. Use TryAdd to get the error.

func (*Decimal) BigInt

func (x *Decimal) BigInt() *big.Int

BigInt gets the `big.Int` representation of the decimal. Panics if x is not an integer. Use `TryBigInt` to catch the error.

func (*Decimal) Clone

func (d *Decimal) Clone() *Decimal

Clone a Decimal

func (*Decimal) Copy

func (x *Decimal) Copy(d *Decimal) *Decimal

Copy set decimal to d

func (*Decimal) Div

func (d *Decimal) Div(d1 *Decimal) *Decimal

Div divides d by d1 (d/d1), panics if there is an error. Use TryDiv to get error

func (*Decimal) Equal

func (d *Decimal) Equal(d1 *Decimal) bool

func (*Decimal) GreaterThan

func (d *Decimal) GreaterThan(d1 *Decimal) bool

func (*Decimal) LessThan

func (d *Decimal) LessThan(d1 *Decimal) bool

func (*Decimal) Mul

func (d *Decimal) Mul(d1 *Decimal) *Decimal

Mul multiply d by d1, panics if there is an error. Use TryMul to get the error.

func (*Decimal) RoundDown added in v1.0.3

func (d *Decimal) RoundDown(places int32) *Decimal

func (*Decimal) RoundUp added in v1.0.3

func (d *Decimal) RoundUp(places int32) *Decimal

func (*Decimal) Set

func (d *Decimal) Set(s string) error

Set is used to support cobra command line.

func (*Decimal) SetUint64

func (d *Decimal) SetUint64(i uint64) *Decimal

SetUint64 set the decimal to the uint64

func (Decimal) String added in v1.0.3

func (d Decimal) String() string

func (*Decimal) Sub

func (d *Decimal) Sub(d1 *Decimal) *Decimal

Sub subtracts d1 from d, panics if there is an error. Use TrySub to get the error.

func (*Decimal) TryAdd

func (d *Decimal) TryAdd(d1 *Decimal) (*Decimal, error)

TryAdd adds d and d1

func (*Decimal) TryBigInt

func (x *Decimal) TryBigInt() (*big.Int, error)

TryBigInt tries to convert the decimal into a `big.Int`. Returns an error if the number is not an integer

func (*Decimal) TryDiv

func (d *Decimal) TryDiv(d1 *Decimal) (*Decimal, error)

TryDiv divides d by d1 (d/d1)

func (*Decimal) TryMul

func (d *Decimal) TryMul(d1 *Decimal) (*Decimal, error)

TryMul multiply d by d1

func (*Decimal) TrySub

func (d *Decimal) TrySub(d1 *Decimal) (*Decimal, error)

TrySub subtracts d1 from d

func (*Decimal) TryUint64

func (x *Decimal) TryUint64() (uint64, error)

TryUint64 returns the uint64 representation of x, if x cannot be represented in an uint64, an error is returned.

func (*Decimal) Type

func (d *Decimal) Type() string

Type is used to support cobra command line.

func (*Decimal) Uint64

func (x *Decimal) Uint64() uint64

Uint64 returns the uint64 representation of x. Panic if x cannot be presented as uint64

func (*Decimal) UnmarshalJSON

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

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

Jump to

Keyboard shortcuts

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