Documentation ¶
Overview ¶
Package currency helps represent a currency with high precision, and do currency computations.
Index ¶
- Variables
- type Currency
- func New(main int, fractional int, code, symbol, funame string, fushare uint) (*Currency, error)
- func NewFractional(ftotal int, code, symbol, funame string, fushare uint) (*Currency, error)
- func ParseFloat64(value float64, code, symbol, funame string, fushare uint) (*Currency, error)
- func ParseString(value string, code, symbol, funame string, fushare uint) (*Currency, error)
- func (c *Currency) Add(acur Currency) error
- func (c *Currency) AddInt(main int, frac int)
- func (c *Currency) Divide(by int, retain bool) ([]Currency, bool)
- func (c *Currency) Float64() float64
- func (c *Currency) FractionalTotal() int
- func (c *Currency) Multiply(by int)
- func (c *Currency) MultiplyFloat64(by float64)
- func (c *Currency) Percent(n float64) *Currency
- func (c *Currency) String(prefixSymbol bool) string
- func (c *Currency) Subtract(scur Currency) error
- func (c *Currency) SubtractInt(main int, frac int)
- func (c *Currency) UpdateWithFractional(frac int)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMismatchCurrency is the error returned if trying to do computation between unmatched currencies ErrMismatchCurrency = errors.New("currencies do not match") // ErrInvalidCurrency is the error returned while trying parse an invalid currency value ErrInvalidCurrency = errors.New("invalid currency value provided") // ErrInvalidFUS is the error returned when Functional unit share is equal to 0 ErrInvalidFUS = errors.New("invalid functional unit share provided") )
Functions ¶
This section is empty.
Types ¶
type Currency ¶
type Currency struct { // Code represents the international currency code Code string `json:"code,omitempty"` // Symbol is the respective currency symbol Symbol string `json:"symbol,omitempty"` // Main represents the main unit value of the currency Main int `json:"main,omitempty"` // Fractional represents the fractional unit value of the currency Fractional int `json:"fractional,omitempty"` // FUName is the name of the fractional unit of the currency. e.g. paise FUName string `json:"fuName,omitempty"` FUShare uint `json:"fuShare,omitempty"` // contains filtered or unexported fields }
Currency represents money with all the meta data required.
func NewFractional ¶
NewFractional returns a new instance of currency given the total value of currency in fractional unit.
func ParseFloat64 ¶
ParseFloat64 will parse a float value into currency.
func ParseString ¶
ParseString will parse a string representation of the currency and return instance of Currency.
func (*Currency) Divide ¶
Divide divides the currency by the given integer and returns a list of currencies and bool. If `retain` is set as true, the balance will not be distributed among the splits, instead retained inside c. It returns a list because, when the currency cannot be split/divided equally, then the remainder has to be distributed. The bool value if `true`, means the currency was split equally.
func (*Currency) FractionalTotal ¶
FractionalTotal returns the total value in fractional int.
func (*Currency) MultiplyFloat64 ¶
MultiplyFloat64 multiplies the currency by a float value.
func (*Currency) SubtractInt ¶
SubtractInt subtracts main & fractional value provided from the currency
func (*Currency) UpdateWithFractional ¶
UpdateWithFractional will update all the relevant values of currency based on the fractional unit provided.