Skip to main content

Module bigdecimal

Module bigdecimal 

Source
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.30.0", 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

Re-exports§

use core::str::FromStr; 🔒
use crate::inspect::PyStaticExpr; 🔒 experimental-inspect
use crate::type_hint_identifier; 🔒 experimental-inspect
use crate::types::PyTuple; 🔒
use crate::Borrowed; 🔒
use crate::Bound; 🔒
use crate::FromPyObject; 🔒
use crate::FromPyObject; 🔒
use crate::IntoPyObject; 🔒
use crate::IntoPyObject; 🔒
use crate::Py; 🔒
use crate::PyAny; 🔒
use crate::PyErr; 🔒
use crate::PyResult; 🔒
use crate::Python; 🔒
use crate::exceptions::PyValueError; 🔒
use crate::sync::PyOnceLock; 🔒
use crate::types::PyAnyMethods; 🔒
use crate::types::PyStringMethods; 🔒
use crate::types::PyType; 🔒
use bigdecimal::BigDecimal; 🔒
use num_bigint::Sign; 🔒
use crate::platform::prelude::*; 🔒

Functions§

get_decimal_cls 🔒
get_invalid_operation_error_cls 🔒
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here