amount

package
v0.0.0-...-88db892 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDecimalIsNil signifies the error when a decimal pointer is nil
	ErrDecimalIsNil = errors.New("unable to perform operation as Decimal is nil")

	// ErrCurrencyNotEnforced signifies the error when the currency is not enforced
	ErrCurrencyNotEnforced = errors.New("currency has not been enforced. Cannot persist this decimal")

	// ErrMultiplicationPanic signifies the error returned when there is a panic during multiplication operation
	ErrMultiplicationPanic = errors.New("panic during product operation due to overflow")

	// ErrDivisionByZero signifies the error returned when there is an error due to an attempt to divide by 0
	ErrDivisionByZero = errors.New("division by zero attempted")

	// ErrDivisionPanic signifies the error returned when there is a panic during division operation
	ErrDivisionPanic = errors.New("panic during division operation")
)
View Source
var DecimalSchemaConverter = func(decoder *schema.Decoder) {
	decoder.RegisterConverter(Decimal{}, converterDecimal)
}

DecimalSchemaConverter allows easy use of this custom type with resttools.Decoder

View Source
var Min = func(d1 Decimal, d2 Decimal) Decimal {
	if d1.Cmp(d2) < 0 {
		return d1
	}
	return d2
}

Min returns the lower decimal

Functions

func EnforceExponentDecimal

func EnforceExponentDecimal(decimalAmount *Decimal, currency common.Currency)

EnforceExponentDecimal is to enforce the currency exponent on an existing decimal value this feature is primarily used when we have an existing decimal but want to ensure the currency exponent is enforced. instances of this could be when we receive decimal from other apps in request or responses. if the exponent in Decimal matches the currency exponent it sets currency enforced to true if not it overwrites the value in Decimal with the required exponent and sets currency enforced to true.

func EnforceExponentDecimalV3

func EnforceExponentDecimalV3(decimalAmount *Decimal, currency common.Currency, exponent int32)

EnforceExponentDecimalV3 is to enforce the currency exponent on an existing decimal value for after v3 grab-api this feature is primarily used when we have an existing decimal but want to ensure the currency exponent is enforced. instances of this could be when we receive decimal from other apps in request or responses. if the exponent in Decimal matches the currency exponent it sets currency enforced to true if not it overwrites the value in Decimal with the required exponent and sets currency enforced to true.

func FormatByCurrencyStandard

func FormatByCurrencyStandard(currency common.Currency, amountDec Decimal) string

FormatByCurrencyStandard formats the decimal value using delimiter used in the currency standard will change only IDR and VND for now

func GetAmountFromMinorUnits

func GetAmountFromMinorUnits(val int64, minorUnits int) float64

GetAmountFromMinorUnits return amount from MinorUnits. input(10023,2)-> (output)100.23.

func GetAmountInMinorUnits

func GetAmountInMinorUnits(val float64, minorUnits int) int64

GetAmountInMinorUnits return amount in MinorUnits. input(100.23745,2)-> (output)10023.

func IsCurrencyValidInDecimal

func IsCurrencyValidInDecimal(currency common.Currency) bool

IsCurrencyValidInDecimal ...

Types

type Decimal

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

Decimal represents a type which can be used to represent amounts. Please note that decimal can represent only the amount and not currency.

Decimal's zero value upon creation is zero i.e. decimal := Decimal{}

The best way to create a new Decimal from float64 is to use NewDecimalFromFloatWithCurrency and NewDecimalFromFloatWithExponent. for example : testDecimal := NewDecimal(testFloatAmount, common.Singapore)

NOTE that this implementation uses shop-spring decimal and provides the same functionality and is an interface of MoneyType

NOTE: This can "only" represent numbers with a maximum of 2^31 digits after the decimal point.

func NewDecimal

func NewDecimal(value float64, currency common.Currency) (decimalAmount Decimal)

NewDecimal creates a Decimal based on float and currency this function assumes that the currency is valid validation of currency needs to be done with GetExponentForCurrency it returns a currencyEnforced Decimal.

func NewDecimalFromIntegerWithExponent

func NewDecimalFromIntegerWithExponent(integerAmt int64, exponent int) Decimal

NewDecimalFromIntegerWithExponent returns a decimal amount from self-defined exponent. Note: prefer NewDecimalFromMinorUnitAmount to use standard ISO exponent. This api is needed only when you get the exponent from some external source. both integerAmt and exponent can be all integers (positive/negative/zero)

func NewDecimalFromMinorUnitAmount

func NewDecimalFromMinorUnitAmount(minorAmount int64, currency common.Currency) (Decimal, error)

NewDecimalFromMinorUnitAmount returns a decimal amount formatted by ISO_4217

func NewDecimalFromString

func NewDecimalFromString(value string, currency common.Currency) (decimalAmount Decimal, err error)

NewDecimalFromString creates a Decimal based on string and currency this function assumes that the currency is valid validation of currency needs to be done with GetExponentForCurrency it returns a currencyEnforced Decimal.

func NewDecimalV1

func NewDecimalV1(value float64, currency common.Currency) (decimalAmount Decimal)

NewDecimalV1 creates a Decimal based on float and currency

func NewScalarDecimal

func NewScalarDecimal(value float64, exp int32) (scalarAmount Decimal)

NewScalarDecimal creates a Decimal based on float and exponent TO BE USED only for scalars (not for amounts) for amounts please use NewDecimal(value float64, currency common.Currency).

func ZeroDecimal

func ZeroDecimal(currency common.Currency) (decimalAmount Decimal)

ZeroDecimal creates a zero value Decimal based on currency this function assumes that the currency is valid

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value of a Decimal

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Add allows the addition of two Decimal types.

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) int

Cmp compares the numbers represented by d and d2 and returns:

-1 if d <  d2
 0 if d == d2
+1 if d >  d2

func (Decimal) DisplayString

func (d Decimal) DisplayString(currency common.Currency) (value string)

DisplayString returns the standard display of the amount with fixed decimal places

func (Decimal) Divide

func (d Decimal) Divide(d2 Decimal) (decimal Decimal, err error)

Divide allows the division of a Decimal type by another Decimal type This function will return an ErrDivisionPanic when a division by zero is attempted It should not return error for other scenarios, kindly let us know if it does division is generally less precise and we would suggest using multiplication where applicable i.e. 6/100 == 6*0.01.

func (Decimal) Equals

func (d Decimal) Equals(d2 Decimal) bool

Equals returns whether the numbers represented by d and d2 are equal.

func (Decimal) Exponent

func (d Decimal) Exponent() int32

Exponent returns the exponent value of the decimal.

func (Decimal) ExponentForDivision

func (d Decimal) ExponentForDivision() int32

ExponentForDivision returns the exponent value of the decimal for division.

func (Decimal) GetFloat64

func (d Decimal) GetFloat64() (value float64)

GetFloat64 returns the float representation of the Decimal.

func (Decimal) GetInt64 deprecated

func (d Decimal) GetInt64() (value int64)

GetInt64 returns the int64 representation of the Decimal.

Deprecated: this will discard the decimal fraction, please use ToMinorUnit().

func (Decimal) GetMinorUnitAmount

func (d Decimal) GetMinorUnitAmount(currency common.Currency) (int64, error)

GetMinorUnitAmount returns the minor unit int amount of the currency powered by ISO_4217 Deprecated: use ToMinorUnit instead.

func (*Decimal) GobDecode

func (d *Decimal) GobDecode(buf []byte) error

GobDecode implements the gob interface.

func (Decimal) GobEncode

func (d Decimal) GobEncode() ([]byte, error)

GobEncode implements the gob interface.

func (Decimal) GreaterOrEquals

func (d Decimal) GreaterOrEquals(d2 Decimal) bool

GreaterOrEquals returns whether the decimal represented by d is greater or equals to d2.

func (Decimal) GreaterThan

func (d Decimal) GreaterThan(d2 Decimal) bool

GreaterThan returns whether the numbers represented by d is greater than d2.

func (Decimal) IsCurrencyEnforced

func (d Decimal) IsCurrencyEnforced() bool

IsCurrencyEnforced allows to check if currency has been enforced on this decimal.

func (Decimal) IsNegative

func (d Decimal) IsNegative() bool

IsNegative returns whether the decimal is lesser than 0.

func (Decimal) IsNonNegative

func (d Decimal) IsNonNegative() bool

IsNonNegative returns whether the decimal is greater than or equal to 0.

func (Decimal) IsNonPositive

func (d Decimal) IsNonPositive() bool

IsNonPositive returns whether the decimal is less than or equal to 0.

func (Decimal) IsPositive

func (d Decimal) IsPositive() bool

IsPositive returns whether the decimal is greater than 0.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns whether the decimal is equal to 0.

func (Decimal) LessThan

func (d Decimal) LessThan(d2 Decimal) bool

LessThan returns whether the numbers represented by d is less than d2.

func (Decimal) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (Decimal) Multiply

func (d Decimal) Multiply(d2 Decimal) (decimal Decimal, err error)

Multiply allows the product of a Decimal type with a Decimal type.

func (*Decimal) Scan

func (d *Decimal) Scan(value interface{}) error

Scan implements the sql.Scanner interface for database deserialization.

func (Decimal) String

func (d Decimal) String() (value string)

String returns the standard display of the amount.

func (Decimal) Subtract

func (d Decimal) Subtract(d2 Decimal) Decimal

Subtract allows the difference of two Decimal types.

func (Decimal) ToMinorUnit

func (d Decimal) ToMinorUnit(currency common.Currency) (int64, error)

ToMinorUnit returns the minor unit int amount of the currency powered by ISO_4217. The decimal amount to convert can be all integers (positive/negative/zero)

func (Decimal) ToggleSign

func (d Decimal) ToggleSign() Decimal

ToggleSign toggles the sign of d.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (Decimal) Value

func (d Decimal) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database serialization.

Jump to

Keyboard shortcuts

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