paupino / rust-decimal

Decimal number implementation written in pure Rust suitable for financial and fixed-precision calculations.
https://docs.rs/rust_decimal/
MIT License
1.03k stars 183 forks source link

`trunc_with_scale` issue #599

Closed moderncodes closed 1 year ago

moderncodes commented 1 year ago

rustc 1.71.0 rust_decimal = "1.30.0" rust_decimal_macros = "1.30.0"

Bellow code example, trc_4 result value from trunc_with_scale does not truncate to defined scale.

use rust_decimal::prelude::*;
use rust_decimal_macros::dec;

fn main() {
    let val_1 = dec!(999.25);
    let val_2 = dec!(1938.03);
    let div_3 = val_1 / val_2;
    let trc_4 = res_3.trunc_with_scale(7); 

    println!("val_1: {:?}; val_2: {:?}; div_3 : {:?}; trc_4: {:?}" ,val_1, val_2, div_3, trc_4);
}

Output

val_1: 999.25; val_2: 1938.03; div_3: 0.5156008936910161349411515818; trc_4: 0.5

Expected

val_1: 999.25; val_2: 1938.03; div_3: 0.5156008936910161349411515818; trc_4: 0.5156008
paupino commented 1 year ago

Thank you for raising this. It appears the algorithm was implicitly rounding when requiring multiple words. Fortunately this is an easy fix - PR incoming.

moderncodes commented 1 year ago

Would you please publish your fix to crates.io, or you follow some process?

paupino commented 1 year ago

Will get a release out today. I was looking at trying to get another feature in there, however haven't found the time. Publishing a small release isn't necessarily a bad thing!

moderncodes commented 1 year ago

Thank you!