Skip to content
Sarj Code Standards

prefer-decimal-money

Carry monetary amounts as Decimal or a money type, not float.

Why

Binary floating-point arithmetic can round monetary amounts and make totals disagree with their component transactions.

Fix

Use Decimal or a dedicated money type and preserve the agreed serialization format at the boundary. Integer minor units are also valid when the contract defines them.

Examples

Before — flagged A purchase price uses binary floating point
app/deals.py
from pydantic import BaseModel
class Deal(BaseModel):
purchase_price: float
After — preferred A purchase price uses decimal arithmetic
app/deals.py
from decimal import Decimal
from pydantic import BaseModel
class Deal(BaseModel):
purchase_price: Decimal