decimal

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 5 Imported by: 5

README

Package github.com/strongo/decimal

Go CI GoDoc Go Report

Decimal 64 bit numbers implementation to represent money values in GoLang. Based on int64. Supports JSON (un)marshalling.

At the moment provides just a single type Decimal64p2 with fixed precision of 2 digits after point. In simple words it stores value as 64 bits integer amount of cents.

The code has 100% unit tests coverage.

E.g. 1.43 will be stored as int64(143) but when rendered as string will be represented as "1.43".

package example

import "github.com/strongo/decimal"

func Example() {
	var amount decimal.Decimal64p2; print(amount)  // 0
	
	amount = decimal.NewDecimal64p2(0, 43); print(amount)  // 0.43
	amount = decimal.NewDecimal64p2(1, 43); print(amount)  // 1.43
	amount = decimal.NewDecimal64p2FromFloat64(23.100001); print(amount)  // 23.10
	amount, _ = decimal.ParseDecimal64p2("2.34"); print(amount)  // 2.34
	amount, _ = decimal.ParseDecimal64p2("-3.42"); print(amount)  // -3.42
}

This package originally was developed for DebtsTracker.io - a mobile app & chat bots to split bills & track your debts.

Reasoning

  • Fast
  • Compact
  • No precision issues with storing values like 0.10
  • By storing with precision to cents there is no ambiguity with rounding. E.g. if you split $10 between 3 persons the amounts will be $3.33, $3.33 & $3.34.

MIT License

Free to use without restrictions. If cloned please keep links to https://github.com/strongo/decimal and to https://debtstracker.io/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal64p2

type Decimal64p2 int64

Decimal64p2 is a decimal number implementation based on int64 with 2 digits after point fixed precision

func NewDecimal64p2

func NewDecimal64p2(intPart int64, decimalPart int8) Decimal64p2

NewDecimal64p2 creates Decimal64p2 from integer and decimal parts

func NewDecimal64p2FromFloat64

func NewDecimal64p2FromFloat64(f float64) Decimal64p2

NewDecimal64p2FromFloat64 creates Decimal64p2 from float64

func NewDecimal64p2FromInt

func NewDecimal64p2FromInt(intPart int) Decimal64p2

NewDecimal64p2FromInt creates Decimal64p2 from integer

func ParseDecimal64p2

func ParseDecimal64p2(s string) (d Decimal64p2, err error)

ParseDecimal64p2 creates Decimal64p2 from a string

func (Decimal64p2) Abs

func (d Decimal64p2) Abs() Decimal64p2

Abs returns absolute value for the decimal

func (Decimal64p2) AsFloat64

func (d Decimal64p2) AsFloat64() float64

AsFloat64 converts decimal to float64

func (Decimal64p2) DecimalPart

func (d Decimal64p2) DecimalPart() int64

DecimalPart returns part after point

func (Decimal64p2) IntPart

func (d Decimal64p2) IntPart() int64

IntPart returns integer part of the decimal

func (Decimal64p2) MarshalJSON

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

MarshalJSON marshals decimal to JSON

func (Decimal64p2) String

func (d Decimal64p2) String() string

String renders decimal to string. If integer the .00 is NOT rendered.

func (*Decimal64p2) UnmarshalJSON

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

UnmarshalJSON deserializes JSON to decimal

Jump to

Keyboard shortcuts

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