gofpdecimal

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: MIT Imports: 5 Imported by: 0

README

go-fpdecimal

一个简单的精度有限的decimal库(或许不应该叫fixed-point)。代码基于fpdecimal,但这个库精度写死了,我又不需要decimal如此大的开销,所以在前者基础上做了些许改动。

目前支持与string,float的相互转换,以及加减、整数乘除。测试的不是很充分,可能会有bug。

install

go get github.com/hesic73/go-fpdecimal

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendFixedPointDecimal

func AppendFixedPointDecimal(b []byte, v int64, p int) []byte

AppendFixedPointDecimal appends formatted fixed point decimal to destination buffer. Returns appended slice. This is efficient for avoiding memory copy. strconv.AppendInt is very efficient. Efficient converting int64 to ASCII is not as trivial.

func Comparator added in v0.0.13

func Comparator(a, b FpDecimal) int

func FixedPointDecimalToString

func FixedPointDecimalToString(v int64, p int) string

FixedPointDecimalToString formats fixed-point decimal to string

func Log10Int added in v0.0.15

func Log10Int(d FpDecimal) int

func ParseFixedPointDecimal

func ParseFixedPointDecimal(s string) (int64, uint, error)

Types

type FpDecimal

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

func Add added in v0.0.5

func Add(a, b FpDecimal) (FpDecimal, error)

Preserve the larger precision. It will throw an error when these efforts fail

func FromFloat64

func FromFloat64(f float64, precision uint) FpDecimal

func FromInt64 added in v0.0.5

func FromInt64(i int64) FpDecimal

func FromString

func FromString(s string) (FpDecimal, error)

func GetOne

func GetOne() FpDecimal

func GetZero

func GetZero() FpDecimal

func NewFpDecimal

func NewFpDecimal(precision uint) FpDecimal

func Sub added in v0.0.5

func Sub(a, b FpDecimal) (FpDecimal, error)

Preserve the larger precision. It will throw an error when these efforts fail

func (FpDecimal) Div added in v0.0.11

func (d FpDecimal) Div(v FpDecimal) (FpDecimal, error)

最开始设计的时候就有问题,我把精度和magnitude混为一谈

func (FpDecimal) DivInteger added in v0.0.8

func (d FpDecimal) DivInteger(i int64) (FpDecimal, error)

no guarantee on precision of the result

func (*FpDecimal) Float32

func (d *FpDecimal) Float32() float32

func (*FpDecimal) Float64

func (d *FpDecimal) Float64() float64

func (*FpDecimal) GetPrecision

func (d *FpDecimal) GetPrecision() uint

func (*FpDecimal) IsZero added in v0.0.9

func (d *FpDecimal) IsZero() bool

func (*FpDecimal) MarshalJSON

func (d *FpDecimal) MarshalJSON() ([]byte, error)

func (FpDecimal) MulInteger added in v0.0.8

func (d FpDecimal) MulInteger(i int64) (FpDecimal, error)

func (FpDecimal) Neg added in v0.0.12

func (d FpDecimal) Neg() FpDecimal

func (FpDecimal) Percent added in v0.0.7

func (d FpDecimal) Percent() string

func (FpDecimal) Sign added in v0.0.12

func (d FpDecimal) Sign() int

func (FpDecimal) String

func (d FpDecimal) String() string

func (*FpDecimal) To added in v0.0.5

func (d *FpDecimal) To(precision uint) bool

func (*FpDecimal) ToPrecision added in v0.0.10

func (d *FpDecimal) ToPrecision(n uint) string

func (*FpDecimal) UnmarshalJSON

func (d *FpDecimal) UnmarshalJSON(b []byte) (err error)

Jump to

Keyboard shortcuts

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