Skip to content

percentage

What percent part is of total, or None when total is zero.

The result is not clamped: a part larger than total gives more than 100.

use helpers4::number::percentage;

Cargo feature number (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features number

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["number"] }
pub fn percentage(part: f64, total: f64) -> Option<f64>
ParameterTypeDescription
partf64The quantity to express as a percentage.
totalf64The whole that part is a share of.

Option<f64>

use helpers4::number::percentage;

assert_eq!(percentage(25.0, 200.0), Some(12.5));
assert_eq!(percentage(3.0, 2.0), Some(150.0));
assert_eq!(percentage(1.0, 0.0), None);

src/number/percentage.rs