Documentation
¶
Index ¶
- Variables
- func EnforceExponentDecimal(decimalAmount *Decimal, currency common.Currency)
- func EnforceExponentDecimalV3(decimalAmount *Decimal, currency common.Currency, exponent int32)
- func FormatByCurrencyStandard(currency common.Currency, amountDec Decimal) string
- func GetAmountFromMinorUnits(val int64, minorUnits int) float64
- func GetAmountInMinorUnits(val float64, minorUnits int) int64
- func IsCurrencyValidInDecimal(currency common.Currency) bool
- type Decimal
- func NewDecimal(value float64, currency common.Currency) (decimalAmount Decimal)
- func NewDecimalFromIntegerWithExponent(integerAmt int64, exponent int) Decimal
- func NewDecimalFromMinorUnitAmount(minorAmount int64, currency common.Currency) (Decimal, error)
- func NewDecimalFromString(value string, currency common.Currency) (decimalAmount Decimal, err error)
- func NewDecimalV1(value float64, currency common.Currency) (decimalAmount Decimal)
- func NewScalarDecimal(value float64, exp int32) (scalarAmount Decimal)
- func ZeroDecimal(currency common.Currency) (decimalAmount Decimal)
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) DisplayString(currency common.Currency) (value string)
- func (d Decimal) Divide(d2 Decimal) (decimal Decimal, err error)
- func (d Decimal) Equals(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) ExponentForDivision() int32
- func (d Decimal) GetFloat64() (value float64)
- func (d Decimal) GetInt64() (value int64)deprecated
- func (d Decimal) GetMinorUnitAmount(currency common.Currency) (int64, error)
- func (d *Decimal) GobDecode(buf []byte) error
- func (d Decimal) GobEncode() ([]byte, error)
- func (d Decimal) GreaterOrEquals(d2 Decimal) bool
- func (d Decimal) GreaterThan(d2 Decimal) bool
- func (d Decimal) IsCurrencyEnforced() bool
- func (d Decimal) IsNegative() bool
- func (d Decimal) IsNonNegative() bool
- func (d Decimal) IsNonPositive() bool
- func (d Decimal) IsPositive() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LessThan(d2 Decimal) bool
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) Multiply(d2 Decimal) (decimal Decimal, err error)
- func (d *Decimal) Scan(value interface{}) error
- func (d Decimal) String() (value string)
- func (d Decimal) Subtract(d2 Decimal) Decimal
- func (d Decimal) ToMinorUnit(currency common.Currency) (int64, error)
- func (d Decimal) ToggleSign() Decimal
- func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error
- func (d Decimal) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
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") )
var DecimalSchemaConverter = func(decoder *schema.Decoder) { decoder.RegisterConverter(Decimal{}, converterDecimal) }
DecimalSchemaConverter allows easy use of this custom type with resttools.Decoder
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 ¶
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 ¶
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 ¶
FormatByCurrencyStandard formats the decimal value using delimiter used in the currency standard will change only IDR and VND for now
func GetAmountFromMinorUnits ¶
GetAmountFromMinorUnits return amount from MinorUnits. input(10023,2)-> (output)100.23.
func GetAmountInMinorUnits ¶
GetAmountInMinorUnits return amount in MinorUnits. input(100.23745,2)-> (output)10023.
func IsCurrencyValidInDecimal ¶
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 ¶
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 ¶
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 ¶
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 ¶
NewDecimalV1 creates a Decimal based on float and currency
func NewScalarDecimal ¶
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 ¶
ZeroDecimal creates a zero value Decimal based on currency this function assumes that the currency is valid
func (Decimal) Cmp ¶
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 ¶
DisplayString returns the standard display of the amount with fixed decimal places
func (Decimal) Divide ¶
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) ExponentForDivision ¶
ExponentForDivision returns the exponent value of the decimal for division.
func (Decimal) GetFloat64 ¶
GetFloat64 returns the float representation of the Decimal.
func (Decimal) GetMinorUnitAmount ¶
GetMinorUnitAmount returns the minor unit int amount of the currency powered by ISO_4217 Deprecated: use ToMinorUnit instead.
func (Decimal) GreaterOrEquals ¶
GreaterOrEquals returns whether the decimal represented by d is greater or equals to d2.
func (Decimal) GreaterThan ¶
GreaterThan returns whether the numbers represented by d is greater than d2.
func (Decimal) IsCurrencyEnforced ¶
IsCurrencyEnforced allows to check if currency has been enforced on this decimal.
func (Decimal) IsNegative ¶
IsNegative returns whether the decimal is lesser than 0.
func (Decimal) IsNonNegative ¶
IsNonNegative returns whether the decimal is greater than or equal to 0.
func (Decimal) IsNonPositive ¶
IsNonPositive returns whether the decimal is less than or equal to 0.
func (Decimal) IsPositive ¶
IsPositive returns whether the decimal is greater than 0.
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Decimal) ToMinorUnit ¶
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) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.