#percentage #decimal #precision #high #rust-decimal

decimal-percentage

Percentage type with rust_decimal

4 releases

0.1.4 Dec 7, 2023
0.1.3 Aug 18, 2021
0.1.2 Mar 17, 2021
0.1.1 May 21, 2020
0.1.0 May 21, 2020

#87 in Math

Download history 3458/week @ 2024-03-14 2463/week @ 2024-03-21 2966/week @ 2024-03-28 2182/week @ 2024-04-04 4159/week @ 2024-04-11 3088/week @ 2024-04-18 4642/week @ 2024-04-25 3496/week @ 2024-05-02 5854/week @ 2024-05-09 4904/week @ 2024-05-16 3154/week @ 2024-05-23 4900/week @ 2024-05-30 4995/week @ 2024-06-06 3579/week @ 2024-06-13 3502/week @ 2024-06-20 1998/week @ 2024-06-27

15,291 downloads per month
Used in 2 crates

MIT license

14KB
243 lines

Percentage Type with Decimal

Build Status MIT Licensed Crates.io decimal-percentage

A type to represent percentage with high precision thanks to rust_decimal.

A percentage can perform addition, subtraction and multiplication.

use decimal_percentage::Percentage;
use rust_decimal::Decimal;

let p1 = Percentage::from(0.1f64);
let p2 = Percentage::from(0.1f32);
let p3 = Percentage::try_from("0.1").unwrap();
let p4 = Percentage::from(Decimal::from_f64(0.3).unwrap());

assert_eq!(p1 + p2, Percentage::from(0.2));
assert_eq!(p1 + 0.2, Percentage::from(0.3));
assert_eq!(p4 - p2, Percentage::from(0.2));
assert_eq!(p1 * 66.0, 6.6);
assert_eq!(p1 * 100u32, 10u32);
assert_eq!(p1 * -100i32, -10i32);
// note that a multiplication to integer type can lose precision
assert_eq!(p1 * -33i32, -3i32);
// multiplication on extremely small value with Decimal,
// that is not representable with float point
let small_value = Decimal::from_str("0.0000000000000000002").unwrap();
assert_eq!(p1 * small_value, Decimal::from_str("0.00000000000000000002").unwrap());

Contributing

Contributions and feed back are welcome following Github workflow.

License

decimal_percentage is provided under the MIT license. See LICENSE.

Dependencies

~1–1.6MB
~35K SLoC