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

`is_integer` has BUG #604

Closed gaoqiangz closed 1 year ago

gaoqiangz commented 1 year ago
assert!(rust_decimal::Decimal::from_str("0.4000000000000000000").unwrap().is_integer()); //will panic
paupino commented 1 year ago

I don't think this will panic right now as is, however I think it should panic. As an alternative example:

let number = dec!(0.4000000000000000000);
println!("Number: {number}");
println!("Is Integer? {}", number.is_integer());

This will output:

Number: 0.4000000000000000000
Is Integer? true

This is incorrect: the is_integer function should be returning false.

It should be a trivial fix - it is likely a rounding issue. To note: dec!(0.400_000_000) is correct however dec!(0.4_000_000_000) will fail.