Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added
- Reference to Martin Fowler's Money Pattern in documentation
- ReadTheDocs integration for comprehensive documentation
- Documentation on working with cents and smallest currency units

### Changed
- Updated documentation URLs to point to ReadTheDocs
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ print(f"Tax: {tax.format()}") # $1.40
print(f"Total: {total.format()}") # $21.39
```

### Working with Cents

Some applications store and calculate money in the smallest currency unit (cents, pence, etc.). Here's how to work with cents in moneyx:

```python
from decimal import Decimal
from moneyx import Money

# Convert from cents to dollars when creating Money objects
cents_amount = 1299 # $12.99 in cents
price = Money(cents_amount / 100, "USD")
print(price.format()) # $12.99

# For more precision, use Decimal
cents_amount = 1299
price = Money(Decimal(cents_amount) / Decimal("100"), "USD")
print(price.format()) # $12.99

# Converting a Money object to cents
dollars = Money("45.67", "USD")
cents = int(dollars.amount * 100) # 4567
print(f"Amount in cents: {cents}")

# Working with cents directly for JPY (which has 0 decimal places)
yen_amount = 1000 # ¥1000 (JPY has 0 decimal places)
jpy = Money(yen_amount, "JPY")
print(jpy.format()) # ¥1,000
```

Note that moneyx handles the smallest currency unit internally, so you generally don't need to worry about cents-to-dollars conversion unless your application specifically stores amounts in cents.

### Different Currencies

```python
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
author = "devAbreu"

# The full version, including alpha/beta/rc tags
release = "0.1.0"
release = "0.1.1"

# Extensions
extensions = [
Expand Down
32 changes: 32 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ Multiply a Money object by a number:
discount = price.multiply(0.20) # $4.00 (20% of price)
discounted = price.subtract(discount) # $15.99

Working with Cents
-----------------

Many financial applications store monetary values in the smallest currency unit (cents, pence, etc.). Here's how to work with cents in moneyx:

.. code-block:: python

from decimal import Decimal
from moneyx import Money

# Converting from cents to dollars
cents_amount = 1299 # $12.99 in cents
price = Money(cents_amount / 100, "USD")
print(price.format()) # $12.99

# For more precision, use Decimal
cents_amount = 1299
price = Money(Decimal(cents_amount) / Decimal("100"), "USD")
print(price.format()) # $12.99

# Converting a Money object to cents
dollars = Money("45.67", "USD")
cents = int(dollars.amount * 100) # 4567
print(f"Amount in cents: {cents}")

# Working with currencies that have 0 decimal places
yen_amount = 1000 # ¥1000 (JPY has 0 decimal places)
jpy = Money(yen_amount, "JPY")
print(jpy.format()) # ¥1,000

Note that moneyx handles the smallest currency unit internally, but converting to/from cents may be necessary when interfacing with other systems or databases that store monetary values as integers.

Rounding Strategies
------------------

Expand Down