paupino / rust-decimal

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

Fixes precision issue with to_f64 with some numbers without fractions #625

Closed paupino closed 9 months ago

paupino commented 9 months ago

Fixes #624

When performing a conversion from Decimal to f64 we would perform a round as the last step to remove any approximations that may have crept in during conversion. This works fairly well when there is a fractional component however is completely unnecessary when the number only has an integral component.

This removes the need to round if dealing with no fractional part.

paupino commented 9 months ago

Hmm - this is an interesting side effect. Will need to look into this. There is a test failing when both serde-str and serde-float are enabled (which is not recommended for use with bincode). I have a longer term approach for richer (de)serialization support via policies, however for the meantime this is unfortunately a supported combination I'll need to investigate.

If, for some reason, you also have serde-float enabled then this will use deserialize_f64 as a type hint. Because converting to f64 loses precision, it's highly recommended that you do NOT enable this feature when working with bincode. That being said, this will only use 8 bytes so is slightly more efficient in terms of storage size.