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.

```rust use decimalpercentage::Percentage; use rustdecimal::Decimal;

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

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

Contributing

Contributions and feed back are welcome following Github workflow.

License

decimal_percentage is provided under the MIT license. See LICENSE.