Money
All money is decimal.Decimal. Floats are rejected with CediTypeError.
cedikit.money
Precise parsing, formatting and word conversion for Ghana cedi amounts.
Every monetary value is a :class:decimal.Decimal (or a :class:Cedi, which
wraps one). Floats are rejected, because they cannot represent pesewas exactly::
>>> 0.1 + 0.2
0.30000000000000004
Example
from cedikit import money money.parse("GH₵1.2k") Decimal('1200.00') money.format("1200.5") 'GH₵ 1,200.50'
Cedi
An immutable cedi amount, always held to the pesewa.
Cedi refuses to mix with floats, so money cannot silently pick up
floating-point error. Arithmetic results are rounded half-up to the pesewa.
Example
price = Cedi("12.50") price * 3 Cedi('37.50') sum([Cedi("1.10"), Cedi("2.20")]) Cedi('3.30') Cedi("10") + 0.5 Traceback (most recent call last): ... cedikit.exceptions.CediTypeError: use Decimal, int or str for money, not float ...
amount
property
The amount as a Decimal with exactly two decimal places.
__truediv__(other)
Divide by a number (giving Cedi) or by another Cedi (giving a Decimal ratio).
to_words()
Spell out this amount; see :func:to_words.
round_pesewas(amount, mode='half_up')
Round an amount to the nearest pesewa (2 decimal places).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
AmountLike
|
The amount to round. |
required |
mode
|
RoundingMode
|
|
'half_up'
|
Example
round_pesewas("2.345") Decimal('2.35') round_pesewas("2.345", mode="bankers") Decimal('2.34')
parse(value)
Parse a cedi amount into a Decimal rounded to the pesewa.
Understands currency markers (GHS, GH₵, GH¢, GHC, ₵,
cedis), thousands separators, compact suffixes (k, m, bn),
pesewa amounts (50p, 50 pesewas) and negatives (-5, (5)).
Raises:
| Type | Description |
|---|---|
MoneyParseError
|
If the text is not a recognisable amount. |
CediTypeError
|
If given a float. |
Example
parse("GHS 1,200.50") Decimal('1200.50') parse("50p") Decimal('0.50') parse("1200 cedis") Decimal('1200.00')
format(amount, style='symbol')
Format a cedi amount for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
AmountLike
|
The amount (Decimal, int, str or Cedi - never float). |
required |
style
|
MoneyStyle
|
|
'symbol'
|
Example
format("1200.5", "code") 'GHS 1,200.50' format("2500000", "compact") 'GH₵ 2.5M'
to_words(amount)
Spell out a cedi amount, as written on cheques and receipts.
Uses British English conventions ("one hundred and five").
Example
to_words("1200.50") 'One thousand two hundred Ghana cedis and fifty pesewas' to_words("1") 'One Ghana cedi' to_words("0.05") 'Five pesewas'