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.3.0", features = ["bigdecimal"] }
bigdecimal = "0.4"

Note that you must use a compatible version of bigdecimal and PyForge. The required bigdecimal version may vary based on the version of PyForge.

§Example

Rust code to create a function that adds one to a BigDecimal

use bigdecimal::BigDecimal;
use pyforge::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