Skip to content

feat(finance): add Decimal support with precision control#1692

Open
rodrigobnogueira wants to merge 1 commit intolk-geimfari:masterfrom
rodrigobnogueira:feature/decimal-support-finance
Open

feat(finance): add Decimal support with precision control#1692
rodrigobnogueira wants to merge 1 commit intolk-geimfari:masterfrom
rodrigobnogueira:feature/decimal-support-finance

Conversation

@rodrigobnogueira
Copy link

@rodrigobnogueira rodrigobnogueira commented Jan 10, 2026

Summary

Add optional Decimal return type for price() and price_in_btc() methods to support high-precision financial calculations.

This implementation uses native Decimal generation via integer arithmetic — no intermediate float representation that could introduce precision errors.

Closes #1575

Changes

  • Add as_decimal: bool = False parameter for backward compatibility
  • Add precision: int parameter to control decimal places (default 2 for price, 8 for BTC)
  • Use native Decimal generation (integer arithmetic, no intermediate float)
  • Add comprehensive tests for new functionality

Example Usage

from mimesis import Finance
from decimal import Decimal

f = Finance()

# Default float (backward compatible)
f.price()  # 1234.56

# High-precision Decimal
f.price(as_decimal=True)  # Decimal('1234.56')

# Custom precision (e.g., 4 decimals for forex)
f.price(precision=4, as_decimal=True)  # Decimal('1234.5678')

# BTC with satoshi precision (8 decimals)
f.price_in_btc(as_decimal=True)  # Decimal('0.12345678')

Testing

  • All 8753 existing tests pass
  • Added new tests for Decimal return type and precision control
  • Verified seeded reproducibility for Decimal outputs

Add optional Decimal return type for price() and price_in_btc() methods
to support high-precision financial calculations.

Changes:
- Add as_decimal parameter (default False) for backward compatibility
- Add precision parameter to control decimal places
- Use native Decimal generation (no intermediate float)
- Add comprehensive tests for new functionality

Closes lk-geimfari#1575
@codecov
Copy link

codecov bot commented Jan 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.91%. Comparing base (64923e6) to head (6a53f6e).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1692      +/-   ##
==========================================
+ Coverage   98.90%   98.91%   +0.01%     
==========================================
  Files          50       50              
  Lines        2098     2119      +21     
==========================================
+ Hits         2075     2096      +21     
  Misses         23       23              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Finance should operate on high precision (Decimal)

1 participant