currency

package
v0.0.0-...-4de7dfb Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 6 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StorjToken is the currency for the STORJ ERC20 token, which powers
	// most payments on the current Storj network.
	StorjToken = New("STORJ Token", "STORJ", 8)
	// USDollars is the currency of United States dollars, where fractional
	// cents are not supported.
	USDollars = New("US dollars", "USD", 2)
	// USDollarsMicro is the currency of United States dollars, where fractional
	// cents are supported with 4 decimal places.
	USDollarsMicro = New("US dollars micro", "USDMicro", 6)
	// Bitcoin is the currency for the well-known cryptocurrency Bitcoin
	// (a.k.a. BTC).
	Bitcoin = New("Bitcoin (BTC)", "BTC", 8)
	// LiveGoats is the currency of live goats, which some Storj network
	// satellites may elect to support for payments.
	LiveGoats = New("Live goats", "goats", 0)

	// Error is a class of errors encountered in the currency package.
	Error = errs.Class("currency")
)

Functions

func DecimalFromBigFloat

func DecimalFromBigFloat(f *big.Float) (decimal.Decimal, error)

DecimalFromBigFloat creates a new decimal.Decimal instance from the given floating point value.

func Greater

func Greater(i, j Amount) (bool, error)

Greater returns true if the first monetary amount is greater than the second. If the currencies are different, an error is thrown.

Types

type Amount

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

Amount represents a currency amount, encapsulating a value and a currency.

The value of the Amount is represented in "base units", meaning units of the smallest indivisible portion of the given currency. For example, when the currency is USDollars, the base unit would be cents.

func Add

func Add(i, j Amount) (Amount, error)

Add adds two monetary amounts and returns the result. If the currencies are different, an error is thrown.

func AmountFromBaseUnits

func AmountFromBaseUnits(units int64, currency *Currency) Amount

AmountFromBaseUnits creates a new Amount instance from the given count of base units and in the given currency.

func AmountFromBigFloat

func AmountFromBigFloat(f *big.Float, currency *Currency) (Amount, error)

AmountFromBigFloat creates a new Amount instance from the given floating point value and in the given currency. The big.Float is expected to give the value of the amount in currency units.

func AmountFromDecimal

func AmountFromDecimal(d decimal.Decimal, currency *Currency) Amount

AmountFromDecimal creates a new Amount instance from the given decimal value and in the given currency. The decimal value is expected to be in currency units.

Example:

AmountFromDecimal(decimal.NewFromFloat(3.50), USDollars) == Amount{baseUnits: 350, currency: USDollars}

func AmountFromString

func AmountFromString(s string, currency *Currency) (Amount, error)

AmountFromString creates a new Amount instance from the given base 10 value and in the given currency. The string is expected to give the value of the amount in currency units.

func (Amount) AsBigFloatWithPrecision

func (a Amount) AsBigFloatWithPrecision(p uint) *big.Float

AsBigFloatWithPrecision returns the monetary value in currency units expressed as an instance of *big.Float with the given precision. _Warning_ this may lose precision if the specified precision is not large enough!

func (Amount) AsDecimal

func (a Amount) AsDecimal() decimal.Decimal

AsDecimal returns the monetary value in currency units expressed as an arbitrary precision decimal number.

func (Amount) AsUnpreciseBigFloat

func (a Amount) AsUnpreciseBigFloat() *big.Float

AsUnpreciseBigFloat returns the monetary value in currency units expressed as an instance of *big.Float with precision=64. _Warning_ may lose precision!

func (Amount) AsUnpreciseFloat

func (a Amount) AsUnpreciseFloat() float64

AsUnpreciseFloat returns the monetary value in currency units expressed as a floating point number. _Warning_ may lose precision! (float64 has the equivalent of 53 bits of precision, as defined by big.Float.)

func (Amount) BaseUnits

func (a Amount) BaseUnits() int64

BaseUnits returns the monetary value expressed in its base units.

func (Amount) Currency

func (a Amount) Currency() *Currency

Currency returns the currency of the amount.

func (Amount) Equal

func (a Amount) Equal(other Amount) bool

Equal returns true if a and other are in the same currency and have the same value.

func (Amount) IsNegative

func (a Amount) IsNegative() bool

IsNegative returns true if the base unit amount is negative.

func (Amount) MarshalJSON

func (a Amount) MarshalJSON() ([]byte, error)

MarshalJSON marshals amount into json.

func (*Amount) UnmarshalJSON

func (a *Amount) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals json bytes into amount.

type Currency

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

Currency represents a currency for the purpose of representing amounts in that currency. Currency instances have a name, a symbol, and a number of supported decimal places of supported precision.

func FromSymbol

func FromSymbol(symbol string) (*Currency, error)

FromSymbol returns currency based on symbol.

func New

func New(name, symbol string, decimalPlaces int32) *Currency

New creates a new Currency instance.

func (*Currency) DecimalPlaces

func (c *Currency) DecimalPlaces() int32

DecimalPlaces returns the decimal places of the currency.

func (*Currency) Name

func (c *Currency) Name() string

Name returns the name of the currency.

func (*Currency) Symbol

func (c *Currency) Symbol() string

Symbol returns the symbol of the currency.

func (*Currency) Zero

func (c *Currency) Zero() Amount

Zero returns the zero value of the currency.

Jump to

Keyboard shortcuts

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