Skip to content

Commit

Permalink
Handling string NaN as Decimal(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorinkoz committed Jun 28, 2024
1 parent 81a79b5 commit 774a576
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pytests/test_money.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def test_init_float():
assert one_million_dollars.amount == Decimal("1000000")


def test_init_nan():
nan_money = Money("NaN")

assert nan_money.amount == Decimal(0)


def test_repr():
assert repr(Money(Decimal("1000000"))) == "Money('1000000')"
assert repr(Money(Decimal("2.000"))) == "Money('2.000')"
Expand Down
5 changes: 5 additions & 0 deletions src/decimals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub fn get_decimal(obj: Bound<PyAny>) -> PyResult<Decimal> {
Ok(amount)
} else if let Ok(f) = obj.extract::<f64>() {
Ok(Decimal::from_f64(f).unwrap())
} else if let Ok(s) = obj.extract::<&str>() {
match s {
"NaN" => Ok(Decimal::new(0, 0)),
_ => Err(PyValueError::new_err("Invalid decimal")),
}
} else {
Err(PyValueError::new_err("Invalid decimal"))
}
Expand Down

0 comments on commit 774a576

Please sign in to comment.