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 182 forks source link

Add documentation for Decimal::to_u<xx>(&self) methods #673

Closed brunbjerg closed 2 months ago

brunbjerg commented 3 months ago

All the methods that takes a reference to a Decimal object and returns an integer type, round the Decimal down to nearest integer, without any explicit mentioning of this. The current documentation for the Decimal::to_u64(&self) -> Option simply says:

rust_decimal::decimal::Decimal
fn to_u64(&self) -> Option<u64>

---
Converts the value of self to a u64. If the value cannot be
represented by a u64, then None is returned.

Would it make sense for me to (at least) add to the method documentations that the calls are dropping the fractional part? Or is this kind of thing implied when using the crate?

Tony-Samuels commented 3 months ago

It matches the behaviour of the standard library in similar situations, but it's easy enough to do so I see no reason not to.

brunbjerg commented 3 months ago

It matches the behaviour of the standard library in similar situations, but it's easy enough to do so I see no reason not to.

Yeah I just test with:

fn main() {
    let float: f64 = 0.5;

    let integer = float as u32;

    println!("{}", integer);
}
$ 0

I believe that the documentation should be added only if there is a clear consensus that the current version is misleading