Documentation ¶
Index ¶
- Constants
- Variables
- func RegisterUnoficialCurrency(code string)
- type Currency
- func (c *Currency) GobDecode(data []byte) error
- func (c Currency) GobEncode() ([]byte, error)
- func (c Currency) MarshalJSON() ([]byte, error)
- func (c Currency) RoundUnit(kind RoundingKind) Decimal
- func (c Currency) Scale() int
- func (c Currency) String() string
- func (c *Currency) UnmarshalJSON(data []byte) error
- func (c Currency) Validate() error
- type CurrencyFormatter
- type Decimal
- func MaxDecimal(first Decimal, rest ...Decimal) Decimal
- func MinDecimal(first Decimal, rest ...Decimal) Decimal
- func MustParseDecimal(value string) Decimal
- func NewDecimal(value float64) (Decimal, error)
- func ParseDecimal(value string) (Decimal, error)
- func Pow10(d Decimal) Decimal
- func Round(x Decimal, unit Decimal, mode RoundingMode) Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Coefficient() big.Int
- func (d Decimal) DeepCopy(dst interface{}) error
- func (d Decimal) Div(d2 Decimal) Decimal
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() float64
- func (d Decimal) Floor() Decimal
- func (d *Decimal) Formatter(scale ...int) number.Formatter
- func (d *Decimal) GobDecode(data []byte) error
- func (d Decimal) GobEncode() ([]byte, error)
- func (d Decimal) IntPart() int64
- func (d Decimal) IsZero() bool
- func (d Decimal) MarshalBinary() (data []byte, err error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Mod(d2 Decimal) Decimal
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d *Decimal) PercentFormatter() number.Formatter
- func (d Decimal) Pow(d2 Decimal) Decimal
- func (d Decimal) Rat() *big.Rat
- func (d Decimal) Round(places int32) Decimal
- func (d Decimal) RoundDown(precision int32) Decimal
- func (d Decimal) RoundNearest(unit Decimal) Decimal
- func (d Decimal) RoundUp(precision int32) Decimal
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Validate() error
- type DecimalFormatter
- type Formatter
- type Money
- type RoundingKind
- type RoundingMode
Constants ¶
const ( // SignPositive is the number returned by Sign() when a decimal is positive SignPositive = 1 // SignNeutral is the number returned by Sign() when a decimal is neutral (0) SignNeutral = 0 // SignNegative is the number returned by Sign() when a decimal is negative SignNegative = -1 )
Variables ¶
var ( // ErrInvalidCurrency indicates that the string is not a valid currency as defined by ISO 4217 ErrInvalidCurrency = errors.New("invalid currency") // ErrUnsupportedCurrency indicates that the currency is not supported ErrUnsupportedCurrency = errors.New("unsupported currency") )
var ( // FormatterNarrowSymbol usess narrow symbols. Overrides Symbol, if present. FormatterNarrowSymbol = currency.NarrowSymbol // FormatterSymbol uses symbols instead of ISO codes, when available. FormatterSymbol = currency.Symbol // FormatterISO uses ISO code as symbol. FormatterISO = currency.ISO )
var ( // ErrInvalidDecimal indicates that the string is not a valid decimal ErrInvalidDecimal = errors.New("invalid decimal") )
Functions ¶
func RegisterUnoficialCurrency ¶
func RegisterUnoficialCurrency(code string)
RegisterUnoficialCurrency registers a currency code that is not a valid ISO 4217 currency code.
This can be used for crypto currency codes, such as ETH, DAI, USDC, ...
Types ¶
type Currency ¶
type Currency string
Currency is represented in code as defined by the ISO 4217 format.
Examples:
- Swiss franc - CHF
- United States dollar - USD
- Euro - EUR
- Polish złoty - PLN
- Bitcoin - XBT
func MustParseCurrency ¶
MustParseCurrency is like ParseCurency, but panics if the given currency unit cannot be parsed. It simplifies safe initialisation of Currency values.
func ParseCurrency ¶
ParseCurrency parses a 3-letter ISO 4217 currency code. It returns an error if it is not well-formed or not a recognised currency code.
Examples:
- CHF
- XBT
func (*Currency) GobDecode ¶
GobDecode implements the gob.GobDecoder interface for gob serialization.
func (Currency) GobEncode ¶
GobEncode implements the gob.GobEncoder interface for gob serialization.
func (Currency) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Currency) RoundUnit ¶
func (c Currency) RoundUnit(kind RoundingKind) Decimal
RoundUnit returns a rounding unit for the given kind
func (*Currency) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type CurrencyFormatter ¶
CurrencyFormatter decorates a given number with formatting options.
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal represents a fixed-point decimal. It is immutable. number = value * 10 ^ exp
func MaxDecimal ¶
MaxDecimal returns the largest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Max(arr[0], arr[1:]...)
This makes it harder to accidentally call Max with 0 arguments.
func MinDecimal ¶
MinDecimal returns the smallest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Min(arr[0], arr[1:]...)
This makes it harder to accidentally call Min with 0 arguments.
func MustParseDecimal ¶
func NewDecimal ¶
NewDecimal creates a Decimal from a float
Example:
NewFromFloat(123.45678901234567).String() // output: "123.4567890123456" NewFromFloat(.00000000000000001).String() // output: "0.00000000000000001"
NOTE: errors occur on NaN, +/-inf
func ParseDecimal ¶
ParseDecimal parses the value which must contain a text representation of a floating-point number. The number of integers after the radix point (fraction) determines the rounding precision.
e.g. 120.0 -> Precision 1 e.g. 123.456 -> Precision 3
func Round ¶
func Round(x Decimal, unit Decimal, mode RoundingMode) Decimal
Round rounds the given amount from the given unit
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) Coefficient ¶
Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (Decimal) Div ¶
Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (*Decimal) Formatter ¶
Formatter returns a language/currency-specific formatter for a floating point decimal
func (*Decimal) GobDecode ¶
GobDecode implements the gob.GobDecoder interface for gob serialization.
func (Decimal) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Decimal) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
func (*Decimal) PercentFormatter ¶
PercentFormatter returns a language-specific formatter for a percent
func (Decimal) Round ¶
Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).
Example:
NewFromFloat(5.45).Round(1).String() // output: "5.5" NewFromFloat(545).Round(-1).String() // output: "550"
func (Decimal) RoundDown ¶
RoundDown rounds the decimal down to the given precision instead of to the nearest even
e.g.: 3.1416 -> f(3) = 3.142 3.1416 -> f(2) = 3.15
func (Decimal) RoundNearest ¶
RoundNearest rounds the decimal to the nearest unit
e.g.: 3.1216 -> f(0.05) = 3.10 3.1416 -> f(0.05) = 3.15
func (Decimal) RoundUp ¶
RoundUp rounds the decimal up to the given precision instead of to the nearest even
e.g.: 3.1416 -> f(3) = 3.142 3.1416 -> f(2) = 3.15
func (Decimal) Truncate ¶
Truncate truncates off digits from the number, without rounding.
NOTE: precision is the last digit that will not be truncated (must be >= 0).
Example:
decimal.NewFromString("123.456").Truncate(2).String() // "123.45"
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation is already used when encoding to text, this method stores that string as []byte
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
type DecimalFormatter ¶
type DecimalFormatter struct { CurrencyFormater CurrencyFormatter Currency Currency Rounding RoundingKind }
DecimalFormatter formats Decimal to its string representation
type Formatter ¶
type Formatter struct { CurrencyFormater CurrencyFormatter Rounding RoundingKind }
Formatter formats Money to its string representation
type Money ¶
Money represents an amount of money for a currency
Money is any item or verifiable record that is generally accepted as payment for goods and services and repayment of debts
func Add ¶
Add returns an amount set to the rounded sum x+y. The precision is set to the larger of x's or y's precision before the operation. Rounding is performed according to the default rounding mode
func Div ¶
Div sets z to the rounded quotient x/y and returns z. Precision, rounding, and accuracy reporting are as for Add. Quo panics with ErrNaN if both operands are zero or infinities.
func Mul ¶
Mul sets z to the rounded product x*y and returns z. Precision, rounding, and accuracy reporting are as for Add. Mul panics with ErrNaN if one operand is zero and the other operand an infinity.
func MustParse ¶
MustParse is like Parse, but panics if the given amount or currency cannot be parsed. It simplifies safe initialisation of Money values.
func Parse ¶
Parse parses amount which must contain a text representation of a floating-point number. The number of integers after the radix point (fraction) determines the mantissa precision.
e.g. 120.0 -> Precision 1 e.g. 123.456 -> Precision 3
It also validates the currency, which must represented in code as defined by the ISO 4217 format.
e.g. CHF -> Swiss franc
func Sub ¶
Sub returns an amount set to the rounded difference x-y. Precision, rounding, and accuracy reporting are as for Add. Sub panics with ErrNaN if x and y are infinities with equal signs.
type RoundingKind ¶
type RoundingKind string
RoundingKind defines a rounding standard for currencies
var ( // RoundingStandard defines standard rounding and formatting for currencies. RoundingStandard RoundingKind = "standard" // RoundingCash defines rounding and formatting standards for cash // transactions. RoundingCash RoundingKind = "cash" // RoundingAccounting defines rounding and formatting standards for // accounting. RoundingAccounting RoundingKind = "accounting" )
type RoundingMode ¶
type RoundingMode string
RoundingMode defines the rounding Mode to apply
const ( // Down rounds down to the previous increment // e.g. decimal: 1.49 increment: 0.1 result: 1.4 RoundDown RoundingMode = "down" // Up rounds up to the next increment // e.g. decimal: 1.41 increment: 0.1 result: 1.5 RoundUp RoundingMode = "up" // ToNearest rounds to the nearest increment // e.g. decimal: 1.45 increment: 0.1 result: 1.5 RoundToNearest RoundingMode = "to_nearest" )