goprices

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 5 Imported by: 0

README

Money manipulation with golang

All the ideas borrowed from: https://github.com/mirumee/prices

thank to https://github.com/Rhymond/go-money, this library was done properly.

Example

  package main
  
  import (
    money "github.com/site-name/go-prices"
    "log"
  )

  money1, err := money.NewMoney(34.56, "USD")
  if err != nil {
    log.Fatalln(err)
  }
  money2, err := money.NewMoney(23, "usd")
  if err != nil {
    log.Fatalln(err)
  }
  
  sum, err := money1.Add(money2)
  if err != nil {
    log.Fatalln(err)
  }
  
  fmt.Println(sum.String())

Documentation

Index

Constants

View Source
const (
	AED = "AED"
	AFN = "AFN"
	ALL = "ALL"
	AMD = "AMD"
	ANG = "ANG"
	AOA = "AOA"
	ARS = "ARS"
	AUD = "AUD"
	AWG = "AWG"
	AZN = "AZN"
	BAM = "BAM"
	BBD = "BBD"
	BDT = "BDT"
	BGN = "BGN"
	BHD = "BHD"
	BIF = "BIF"
	BMD = "BMD"
	BND = "BND"
	BOB = "BOB"
	BRL = "BRL"
	BSD = "BSD"
	BTN = "BTN"
	BWP = "BWP"
	BYN = "BYN"
	BYR = "BYR"
	BZD = "BZD"
	CAD = "CAD"
	CDF = "CDF"
	CHF = "CHF"
	CLF = "CLF"
	CLP = "CLP"
	CNY = "CNY"
	COP = "COP"
	CRC = "CRC"
	CUC = "CUC"
	CUP = "CUP"
	CVE = "CVE"
	CZK = "CZK"
	DJF = "DJF"
	DKK = "DKK"
	DOP = "DOP"
	DZD = "DZD"
	EEK = "EEK"
	EGP = "EGP"
	ERN = "ERN"
	ETB = "ETB"
	EUR = "EUR"
	FJD = "FJD"
	FKP = "FKP"
	GBP = "GBP"
	GEL = "GEL"
	GGP = "GGP"
	GHC = "GHC"
	GHS = "GHS"
	GIP = "GIP"
	GMD = "GMD"
	GNF = "GNF"
	GTQ = "GTQ"
	GYD = "GYD"
	HKD = "HKD"
	HNL = "HNL"
	HRK = "HRK"
	HTG = "HTG"
	HUF = "HUF"
	IDR = "IDR"
	ILS = "ILS"
	IMP = "IMP"
	INR = "INR"
	IQD = "IQD"
	IRR = "IRR"
	ISK = "ISK"
	JEP = "JEP"
	JMD = "JMD"
	JOD = "JOD"
	JPY = "JPY"
	KES = "KES"
	KGS = "KGS"
	KHR = "KHR"
	KMF = "KMF"
	KPW = "KPW"
	KRW = "KRW"
	KWD = "KWD"
	KYD = "KYD"
	KZT = "KZT"
	LAK = "LAK"
	LBP = "LBP"
	LKR = "LKR"
	LRD = "LRD"
	LSL = "LSL"
	LTL = "LTL"
	LVL = "LVL"
	LYD = "LYD"
	MAD = "MAD"
	MDL = "MDL"
	MKD = "MKD"
	MMK = "MMK"
	MNT = "MNT"
	MOP = "MOP"
	MUR = "MUR"
	MVR = "MVR"
	MWK = "MWK"
	MXN = "MXN"
	MYR = "MYR"
	MZN = "MZN"
	NAD = "NAD"
	NGN = "NGN"
	NIO = "NIO"
	NOK = "NOK"
	NPR = "NPR"
	NZD = "NZD"
	OMR = "OMR"
	PAB = "PAB"
	PEN = "PEN"
	PGK = "PGK"
	PHP = "PHP"
	PKR = "PKR"
	PLN = "PLN"
	PYG = "PYG"
	QAR = "QAR"
	RON = "RON"
	RSD = "RSD"
	RUB = "RUB"
	RUR = "RUR"
	RWF = "RWF"
	SAR = "SAR"
	SBD = "SBD"
	SCR = "SCR"
	SDG = "SDG"
	SEK = "SEK"
	SGD = "SGD"
	SHP = "SHP"
	SKK = "SKK"
	SLL = "SLL"
	SOS = "SOS"
	SRD = "SRD"
	SSP = "SSP"
	STD = "STD"
	SVC = "SVC"
	SYP = "SYP"
	SZL = "SZL"
	THB = "THB"
	TJS = "TJS"
	TMT = "TMT"
	TND = "TND"
	TOP = "TOP"
	TRL = "TRL"
	TRY = "TRY"
	TTD = "TTD"
	TWD = "TWD"
	TZS = "TZS"
	UAH = "UAH"
	UGX = "UGX"
	USD = "USD"
	UYU = "UYU"
	UZS = "UZS"
	VEF = "VEF"
	VND = "VND"
	VUV = "VUV"
	WST = "WST"
	XAF = "XAF"
	XAG = "XAG"
	XAU = "XAU"
	XCD = "XCD"
	XDR = "XDR"
	XPF = "XPF"
	YER = "YER"
	ZAR = "ZAR"
	ZMW = "ZMW"
	ZWD = "ZWD"
)

most common currency units

Variables

View Source
var (
	ErrNotSameCurrency   = errors.New("not same currency")     // ErrNotSameCurrency is used when perform operations between money with different currencies
	ErrUnknownType       = errors.New("unknown given type")    // ErrUnknownType is returned when a type is invalid
	ErrUnknownCurrency   = errors.New("unknown currency unit") // ErrUnknownCurrency is returned when given currency unit is invalid
	ErrNillValue         = errors.New("argument must not be nil")
	ErrDivisorZero       = errors.New("divisor must not be zero")
	ErrInvalidRounding   = errors.New("invalid rounding")
	ErrMoneyNegative     = errors.New("money amount can not be negative")
	ErrStopLessThanStart = errors.New("stop must be greater than start")
)
View Source
var CurrenciesMap = map[string]string{}/* 160 elements not displayed */

CurrenciesMap has keys are currency codes of most countries in the world. Values are full names of according currencies.

Functions

func FixedDiscount

func FixedDiscount[K MoneyObject, T MoneyInterface[K]](base T, discount Money) (K, error)

FixedDiscount applys a fixed discount to any price type.

func FractionalDiscount

func FractionalDiscount[K MoneyObject, T MoneyInterface[K]](base T, fraction decimal.Decimal, fromGross bool) (K, error)

FractionalDiscount Apply a fractional discount based on either gross or net amount

func GetCurrencyPrecision

func GetCurrencyPrecision(currency string) (int, error)

GetCurrencyPrecision returns a number for money rounding.

Returned error could be `nil` or `ErrUnknownCurrency`

E.g:

GetCurrencyPrecision("vnd") => 0, nil

func PercentageDiscount

func PercentageDiscount[K MoneyObject, T MoneyInterface[K]](base T, percentage float64, fromGross bool) (K, error)

PercentageDiscount Apply a percentage discount based on either gross or net amount.

func QuantizePrice

func QuantizePrice[K MoneyObject, T MoneyInterface[K]](price T, round Rounding) (K, error)

QuantizePrice accepts the `price` argument to be either: *TaxedMoney or *Money or *TaxedMoneyRange

Types

type Currencier added in v1.4.2

type Currencier interface {
	GetCurrency() string
}

type Currency

type Currency struct {
	Code        string
	NumericCode string
	Fraction    int
	Grapheme    string
	Template    string
	Decimal     string
	Thousand    string
}

Currency represents money currency information required for formatting.

type Money

type Money struct {
	Amount   decimal.Decimal
	Currency string
}

Money represents an amount of a particular currency.

func NewMoney

func NewMoney(amount float64, currency string) (*Money, error)

NewMoney returns new Money object

func NewMoneyFromDecimal added in v1.4.2

func NewMoneyFromDecimal(amount decimal.Decimal, currency string) (*Money, error)

func (*Money) Add

func (m *Money) Add(other Money) (*Money, error)

Add adds two money amount together, returns new money. If returned error is not nil, it could be ErrNotSameCurrency

func (*Money) Equal

func (m *Money) Equal(other Money) bool

Equal checks if other's amount is equal to m's amount

func (*Money) GetCurrency added in v1.4.2

func (m *Money) GetCurrency() string

GetCurrency returns current money's Currency

func (*Money) LessThan

func (m *Money) LessThan(other Money) bool

LessThan checks if other's amount is greater than m's amount AND checking same currency included

func (*Money) LessThanOrEqual

func (m *Money) LessThanOrEqual(other Money) bool

LessThanOrEqual check if m's amount is less than or equal to other's amount

func (*Money) Mul

func (m *Money) Mul(other float64) Money

Mul multiplty current money with the givent other.

NOTE: other must be either ints or floats or Decimal

func (*Money) Quantize

func (m *Money) Quantize(round Rounding, exp int) (*Money, error)

Return a copy of the object with its amount quantized. NOTE: if exp < 0, default will be used

func (*Money) SameKind

func (m *Money) SameKind(other Money) bool

SameKind checks if other's currency is identical to current money currency. If other is nil, returns false.

func (*Money) String

func (m *Money) String() string

String implements fmt.Stringer interface

func (*Money) Sub

func (m *Money) Sub(other Money) (*Money, error)

Sub subtracts current money to given other. If error is not nil, it could be ErrNotSameCurrency

func (*Money) TrueDiv

func (m *Money) TrueDiv(other float64) Money

TrueDiv divides money with the given other.

NOTE: other must be either ints or uints or floats or Decimal or Money

type MoneyInterface added in v1.4.0

type MoneyInterface[T MoneyObject] interface {
	Quantize(round Rounding, exp int) (T, error) // NOTE: if exp < 0, system wil use default
	fmt.Stringer

	GetCurrency() string
	// contains filtered or unexported methods
}

MoneyInterface

type MoneyObject added in v1.4.0

type MoneyObject interface {
	*Money |
		*MoneyRange |
		*TaxedMoney |
		*TaxedMoneyRange
}

type MoneyRange

type MoneyRange struct {
	Start Money
	Stop  Money
}

MoneyRange has start and stop ends

func NewMoneyRange

func NewMoneyRange(start, stop Money) (*MoneyRange, error)

NewMoneyRange returns a new range. If start is greater than stop or start and stop have different currencies, return nil and non nil error

func (*MoneyRange) Add

func (m *MoneyRange) Add(other any) (*MoneyRange, error)

Add adds a Value to current.

other must be either Money or MoneyRange

func (*MoneyRange) Contains

func (m *MoneyRange) Contains(value Money) bool

Contains check if a Money is between this MoneyRange's two ends

func (*MoneyRange) Equal

func (m *MoneyRange) Equal(other MoneyRange) bool

Equal Checks if two MoneyRange are equal both `Start`, `Stop` and `Currency`

func (*MoneyRange) GetCurrency added in v1.4.2

func (m *MoneyRange) GetCurrency() string

GetCurrency returns current money range's Currency

func (*MoneyRange) LessThan

func (m *MoneyRange) LessThan(other MoneyRange) bool

LessThan compares currenct money range to given other

func (*MoneyRange) LessThanOrEqual

func (m *MoneyRange) LessThanOrEqual(other MoneyRange) bool

LessThanOrEqual checks if current money range is less than or equal given other

func (*MoneyRange) Mul added in v1.4.0

func (m *MoneyRange) Mul(other float64) MoneyRange

func (*MoneyRange) Quantize

func (m *MoneyRange) Quantize(round Rounding, exp int) (*MoneyRange, error)

Return a copy of the range with start and stop quantized. NOTE: if exp < 0 the system will use default

func (*MoneyRange) Replace

func (m *MoneyRange) Replace(start, stop *Money) (*MoneyRange, error)

Replace replace Start and Stop of currenct MoneyRagne With two given `start` and `stop` respectively.

func (*MoneyRange) String

func (m *MoneyRange) String() string

String implements fmt.Stringer any

func (*MoneyRange) Sub

func (m *MoneyRange) Sub(other any) (*MoneyRange, error)

Sub subtracts current money to given `other`. `other` can be either `Money` or `MoneyRange`

func (*MoneyRange) TrueDiv added in v1.4.0

func (m *MoneyRange) TrueDiv(other float64) MoneyRange

type RoundFunc added in v1.3.0

type RoundFunc func(places int32) decimal.Decimal

type Rounding

type Rounding uint8
const (
	Up Rounding = iota
	Down
	Ceil
	Floor
)

type TaxedMoney

type TaxedMoney struct {
	Net   Money
	Gross Money
}

TaxedMoney represents taxed money. It wraps net, gross money and currency.

func NewTaxedMoney

func NewTaxedMoney(net, gross Money) (*TaxedMoney, error)

NewTaxedMoney returns new TaxedMoney, If net and gross have different currency type, return nil and error

func (*TaxedMoney) Add

func (t *TaxedMoney) Add(other any) (*TaxedMoney, error)

Add adds a money or taxed money to this. other must be either Money or TaxedMoney

func (*TaxedMoney) Equal

func (t *TaxedMoney) Equal(other TaxedMoney) bool

Equal checks if two taxed money are equal both in net and gross

func (*TaxedMoney) GetCurrency added in v1.4.2

func (m *TaxedMoney) GetCurrency() string

GetCurrency returns current taxed money's Currency

func (*TaxedMoney) LessThan

func (t *TaxedMoney) LessThan(other TaxedMoney) bool

LessThan check if this money's gross is less than other's gross

func (*TaxedMoney) LessThanOrEqual

func (t *TaxedMoney) LessThanOrEqual(other TaxedMoney) bool

LessThanOrEqual checks if this money is less than or equal to other.

func (*TaxedMoney) Mul

func (m *TaxedMoney) Mul(other float64) TaxedMoney

Mul multiplies current taxed money with given other

other must only be either ints or floats or Decimal

func (*TaxedMoney) Quantize

func (t *TaxedMoney) Quantize(round Rounding, exp int) (*TaxedMoney, error)

Return a new instance with both net and gross quantized. All arguments are passed to `Money.quantize

func (*TaxedMoney) String

func (t *TaxedMoney) String() string

String implements fmt.Stringer interface

func (*TaxedMoney) Sub

func (t *TaxedMoney) Sub(other any) (*TaxedMoney, error)

Add substract this money to other. other must be either Money or TaxedMoney.

func (*TaxedMoney) Tax

func (t *TaxedMoney) Tax() *Money

Tax calculates taxed money by subtracting m's gross to m's net

func (*TaxedMoney) TrueDiv

func (t *TaxedMoney) TrueDiv(other float64) TaxedMoney

TrueDiv divides current tabled money to other. other must be either Decimal or ints or floats

type TaxedMoneyRange

type TaxedMoneyRange struct {
	Start TaxedMoney
	Stop  TaxedMoney
}

func NewTaxedMoneyRange

func NewTaxedMoneyRange(start, stop TaxedMoney) (*TaxedMoneyRange, error)

NewTaxedMoneyRange create new taxed money range. It returns nil and error value if start > stop or they have different currencies

func (*TaxedMoneyRange) Add

func (t *TaxedMoneyRange) Add(other any) (*TaxedMoneyRange, error)

Add adds this taxed money range to another value other must be either: Money, MoneyRange or TaxedMoneyRange or TaxedMoney

func (*TaxedMoneyRange) Contains

func (t *TaxedMoneyRange) Contains(item TaxedMoney) bool

Contains check is given taxed money is in range from start to stop.

start <= item <= stop

func (*TaxedMoneyRange) Equal

func (t *TaxedMoneyRange) Equal(other TaxedMoneyRange) bool

Equal compares two taxed money range

func (*TaxedMoneyRange) GetCurrency added in v1.4.2

func (t *TaxedMoneyRange) GetCurrency() string

GetCurrency returns current taxed money range's Currency

func (*TaxedMoneyRange) LessThan

func (t *TaxedMoneyRange) LessThan(other TaxedMoneyRange) bool

LessThan checks if current taxed money range less than given other

func (*TaxedMoneyRange) LessThanOrEqual

func (t *TaxedMoneyRange) LessThanOrEqual(other TaxedMoneyRange) bool

LessThanOrEqual checks if current taxed money range less than or equal to given other

func (*TaxedMoneyRange) Mul added in v1.4.0

func (t *TaxedMoneyRange) Mul(other float64) TaxedMoneyRange

func (*TaxedMoneyRange) Quantize

func (t *TaxedMoneyRange) Quantize(round Rounding, exp int) (*TaxedMoneyRange, error)

Return a copy of the range with start and stop quantized. NOTE: if exp < 0; default will be used

func (*TaxedMoneyRange) Replace

func (t *TaxedMoneyRange) Replace(start, stop *TaxedMoney) (*TaxedMoneyRange, error)

Return a range with start or stop replaced with given values

func (*TaxedMoneyRange) String

func (t *TaxedMoneyRange) String() string

String implements fmt.Stringer interface

func (*TaxedMoneyRange) Sub

func (t *TaxedMoneyRange) Sub(other any) (*TaxedMoneyRange, error)

Sub substract this taxed money range to given other. other must be either Money or TaxedMoney or MoneyRange or TaxedMoneyRange

func (*TaxedMoneyRange) TrueDiv added in v1.4.0

func (t *TaxedMoneyRange) TrueDiv(other float64) *TaxedMoneyRange

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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