3 个版本
| 0.1.3 | 2023年10月7日 | 
|---|---|
| 0.1.2 | 2023年10月7日 | 
| 0.1.1 | 2023年10月7日 | 
| 0.1.0 |  | 
#14 在 #rounding
19KB
283 行
round_to_int
将浮点数四舍五入为整数。
用法
您可以显式地四舍五入到 i32 和 i64
use round_to_int::*;
assert_eq!(0.4.round_to_i32(), 0);
assert_eq!(0.5.round_to_i64(), 1);
或者隐式地到 i8、i16、i32、i64、i128、isize、u8、u16、u32、u64、u128 或 usize
use round_to_int::*;
let a: i8 = 0.4.round_to();
assert_eq!(a, 0);
使用这些模式
use round_to_int::*;
assert_eq!(0.5.round_to_i32(), 1);
assert_eq!(0.5.floor_to_i32(), 0);
assert_eq!(0.5.ceil_to_i32(), 1);
实现
截至目前,所有内容都是使用 round、floor 和 ceil 实现的。当 round_ties_even 被解决时,这个crate将选择使用它。
将来可能会添加优化实现。