#integer #divide #round

no-std rounded-div

Get rounded result of an integer division

5 releases

0.1.3 Feb 21, 2025
0.1.2 Apr 18, 2021
0.1.1 Apr 17, 2021
0.1.0 Apr 17, 2021
0.0.0 Apr 17, 2021

#1031 in Algorithms

Download history 71/week @ 2024-12-08 47/week @ 2024-12-15 46/week @ 2024-12-22 25/week @ 2024-12-29 116/week @ 2025-01-05 162/week @ 2025-01-12 4156/week @ 2025-01-19 79/week @ 2025-01-26 76/week @ 2025-02-02 123/week @ 2025-02-09 206/week @ 2025-02-16 249/week @ 2025-02-23 92/week @ 2025-03-02 126/week @ 2025-03-09 511/week @ 2025-03-16 135/week @ 2025-03-23

883 downloads per month
Used in 4 crates

MIT license

7KB
55 lines

rounded-div

Test Crates.io Version

Get rounded result of an integer division.

Usage Examples

const fn rounded_div_*

assert_eq!(rounded_div::i32( 59, 4),  15); // 59/4 is equal to 14.75 which is closer to 15
assert_eq!(rounded_div::i32( 58, 4),  15); // 58/4 is equal to 14.5 which is rounded to 15
assert_eq!(rounded_div::i32( 57, 4),  14); // 57/4 is equal to 14.25 which is closer to 14
assert_eq!(rounded_div::i32(-59, 4), -15);
assert_eq!(rounded_div::i32(-58, 4), -15);
assert_eq!(rounded_div::i32(-57, 4), -14);

trait RoundedDiv

use rounded_div::RoundedDiv;
assert_eq!( 59i32.rounded_div(4),  15); // 59/4 is equal to 14.75 which is closer to 15
assert_eq!( 58i32.rounded_div(4),  15); // 58/4 is equal to 14.5 which is rounded to 15
assert_eq!( 57i32.rounded_div(4),  14); // 57/4 is equal to 14.25 which is closer to 14
assert_eq!(-59i32.rounded_div(4), -15);
assert_eq!(-58i32.rounded_div(4), -15);
assert_eq!(-57i32.rounded_div(4), -14);

License

MIT © Hoàng Văn Khải.

No runtime deps