Rust Decimal manipulation support?

I need a variable type in Rust to store decimal numbers, and ability to operates it easily like:

let a = Decimal("120.0");
let b = Decimal("20.05");
let c = Decimal("1.05") + a + b; // c will be 141.1

Rust Decimals (GitHub - paupino/rust-decimal: A Decimal Implementation written in pure Rust suitable for financial calculations.) as far as I know doesn't have this function. Is it possible to "overwrite" the basic math operations so it will be compatible with Decimals ? or is there any other way around this ?

What makes you think that?

ha.... it works. My bad....

    let a = Decimal::from_str("2.02").unwrap();
    let b = Decimal::from_str("5.02").unwrap();
    let c = a + b * b;
    println!("{}", c.to_string());

You know what... this line of documentation about math (rust_decimal - Rust) is not that clear, especially on the etc part, so I assume it won't work before even trying it. And lack of example also doesn't help either

This feature enables mathematical functionality such as pow , ln , enf etc.

1 Like

All the math operators are implemented using traits in the std::ops module. If you scroll to the bottom of the Decimal type's API docs you will see lines like impl Add<Decimal> for Decimal which means you can use the + operator with two Decimals.