API Reference

Currency classes

All currency classes inherit methods from a base Currency class.

A list of available currency classes can be found here.

Base methods

The following methods are for use with the currency instance:

formatted(self) str

Returns a string of the value in the currency format standard.

e.g.

>>> BRL(10).formatted()
'R$ 10,00'
as_decimal(self) Decimal

Returns de decimal value of the currency object.

e.g.

>>> BRL(10).as_decimal()
Decimal('10')

Class methods

sum(values: Sequence[Self]) Self

Equivalent to builtin sum. Recieves a sequence of Currency instances, and returns its sum if all of them are from from the same class calling the method

Parameters:

values – Sequence of Currency values

e.g.

>>> values = [USD(x) for x in (5, 6, 1.5)]
>>> USD.sum(values)
<USD 12.50>
mean(values: Sequence[Self]) Self

Arithmetic mean of a sequence of Currency instances.

Parameters:

values – Sequence of Currency values

e.g.

>>> values = [USD(x) for x in (10, 10, 7)]
>>> USD.mean(values)
<USD 9.00>
set_rates(rates: Dict[str, int | float | Decimal])

Set conversion rates for other currencies.

e.g.

>>> from monepy import JPY
>>> JPY.set_rates({"EUR": 0.0063957, "USD": 0.0067021})
Parameters:

rates – Dictionary containing the currency class name (code) as key and the convertion rate as value.

from_conversion(value: Self, base: type[Self] | None = None) Self

Create a new instance of the by converting an instance of another currency

Parameters:
  • value – Currency object

  • base – Currency class to be used as a base for conversion

Utils

Those are available functions to use with Currency classes, and can be imported from monepy.utils.

e.g.:

>>> from monepy.utils import convert
...  # Import currencies and set conversion rates
>>> convert(USD(10), EUR)
<EUR 9,49>
convert(value: Currency, currency: type[Currency], base: type[Currency] | None = None) Currency

Converts the currency object using pre-configured conversion rates.

>>> convert(USD(10), EUR)
<EUR 9,49>

When using a base currency, the value will be first converted to the base, considering it’s smallest subunit, and then converted to the desired currency.

>>> convert(USD(10), EUR, base=JPY)
<EUR 9,54>
Parameters:
  • value – Currency object

  • currency – New currency class to be converted to

  • base – Currency class to be used as a base for conversion

Available classes

These currency classes can be imported directly from the monepy module:

>>> from monepy import USD
>>> x = USD(10)
>>> x
<USD 10.00>
class BRL(value: int | float | Decimal)

Class to represent Brazilian real

class EUR(value: int | float | Decimal)

Class to represent Euro

class GBP(value: int | float | Decimal)

Class to represent UK Sterling

class JPY(value: int | float | Decimal)

Class to represent Japanese yen

class USD(value: int | float | Decimal)

Class to represent US dollar