#numbers #duration #rounding #round #no-alloc

no-std roundable

将数字和持续时间四舍五入到指定的因子

3 个不稳定版本

0.2.0 2024年3月18日
0.1.1 2024年3月15日
0.1.0 2024年3月14日

181日期和时间

MIT/Apache

50KB
652

将数字和持续时间四舍五入到指定的因子

docs.rs Crates.io Rust version 1.56.1+

此库提供了对各种值的四舍五入实现,包括原生数字类型和 core::time::Duration(也称为 std::time::Duration)。

Roundable trait 为可四舍五入的值添加了以下功能

示例

use roundable::{Roundable, Tie};

assert!(310 == 314.round_to(10, Tie::Up));
assert!(300.0 == 314.1.round_to(100.0, Tie::Up));

// To avoid panicking on overflow:
assert!(Some(260) == 255.try_round_to(10, Tie::Up));
assert!(None == 255u8.try_round_to(10, Tie::Up));

平局策略

“平局”是指两个四舍五入数字之间的确切数值,例如四舍五入到最接近的整数时的 0.5。传统上,平局是通过选择较大的数字来解决的,但还有其他策略。 Roundable 支持以下规则

Duration进行舍入

Duration可以像数字类型一样舍入到Duration因子。为了方便,提供了一些常量,可以用来简化对Duration的舍入。

use roundable::{SECOND, MINUTE, Roundable, Tie};
use std::time::Duration;

assert!(Duration::ZERO == Duration::from_millis(314).round_to(SECOND, Tie::Up));
assert!(MINUTE == Duration::from_millis(59_500).round_to(SECOND, Tie::Up));

默认为#![no_std]

您可以使用此包,无论是否使用stdalloc。您无需启用或禁用任何功能。

⚠️ 开发状态

此包处于积极开发中。API可能完全重写。我愿意接受建议

最低支持的Rust版本

目前最低支持的Rust版本(MSRV)是1.56.1。MSRV的任何未来增加都将需要主版本号的增加。

许可证

此项目同时受Apache 2和MIT许可证的许可。您可以选择使用任一许可证。

贡献

除非您明确声明,否则您提交的任何贡献,根据Apache 2.0许可证定义,应如上所述双许可,不附加任何额外条款或条件。

无运行时依赖