#percentage #save #external #decimal

percentage

A crate to calculate percentages

1 unstable release

0.1.0 Sep 12, 2019

#1076 in Data structures

Download history 6144/week @ 2023-06-06 6698/week @ 2023-06-13 7007/week @ 2023-06-20 6935/week @ 2023-06-27 7939/week @ 2023-07-04 9658/week @ 2023-07-11 7324/week @ 2023-07-18 7287/week @ 2023-07-25 6637/week @ 2023-08-01 7754/week @ 2023-08-08 13469/week @ 2023-08-15 14379/week @ 2023-08-22 13898/week @ 2023-08-29 14709/week @ 2023-09-05 13633/week @ 2023-09-12 12441/week @ 2023-09-19

57,306 downloads per month
Used in 359 crates (6 directly)

MIT/Apache

9KB
129 lines

Percentage

percentage is a crate trying to make using percentages in a safer way and easier to debug. Whenever you see a Percentage, you will know what is being calculated, instead of having to revise the code.

Example

// You only need to import the `Percentage` struct
use percentage::Percentage;

// Here we create the percentage to apply
let percent = Percentage::from(50);

println!("{}", percent.value()); // Will print '50'

// We can apply the percent to any number we want
assert_eq!(15, percent.apply_to(30));
println!("50% of 30 is: {}", percent.apply_to(30)); // Will print '50% of 30 is: 15'

// If you need to use floating points for the percent, you can use `from_decimal` instead

let percent = Percentage::from_decimal(0.5);
assert_eq!(15.0, percent.apply_to(30.0));
println!("50% of 30.0 is: {}", percent.apply_to(30.0)); // Will print '50% of 30.0 is: 15.0'

Dependencies

~475KB