Available on crate feature
bigdecimal
only.Expand description
Conversions to and from bigdecimal’s [BigDecimal
] type.
This is useful for converting Python’s decimal.Decimal into and from a native Rust type.
§Setup
To use this feature, add to your Cargo.toml
:
[dependencies]
pyo3 = { version = "0.25.0-dev", features = ["bigdecimal"] }
bigdecimal = "0.4"
Note that you must use a compatible version of bigdecimal and PyO3. The required bigdecimal version may vary based on the version of PyO3.
§Example
Rust code to create a function that adds one to a BigDecimal
use bigdecimal::BigDecimal;
use pyo3::prelude::*;
#[pyfunction]
fn add_one(d: BigDecimal) -> BigDecimal {
d + 1
}
#[pymodule]
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(add_one, m)?)?;
Ok(())
}
Python code that validates the functionality
from my_module import add_one
from decimal import Decimal
d = Decimal("2")
value = add_one(d)
assert d + 1 == value