#calculate #percent #percentages

percentage

A crate to calculate percentages

1 unstable release

0.1.0 Sep 12, 2019

#11 in #percent

Download history 30097/week @ 2024-11-17 27503/week @ 2024-11-24 42436/week @ 2024-12-01 36773/week @ 2024-12-08 28871/week @ 2024-12-15 15090/week @ 2024-12-22 20982/week @ 2024-12-29 37181/week @ 2025-01-05 40080/week @ 2025-01-12 42250/week @ 2025-01-19 41733/week @ 2025-01-26 48117/week @ 2025-02-02 45725/week @ 2025-02-09 38608/week @ 2025-02-16 41442/week @ 2025-02-23 42643/week @ 2025-03-02

171,924 downloads per month
Used in 727 crates (12 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

~470KB