Skip to main content

percent_text

Function percent_text 

Source
pub fn percent_text(ratio: f64) -> String
Expand description

A ratio as a signed percentage with one decimal: 0.2 is +20.0%, -0.05 is -5.0%. Anything that rounds to zero at one decimal prints 0.0% with no sign, so a surface never shows -0.0% for a result that is, to the precision shown, no change. A value that is not a number prints n/a rather than a figure.

use majordomus_cli::economics::stats::percent_text;
assert_eq!(percent_text(0.2), "+20.0%");
assert_eq!(percent_text(-0.05), "-5.0%");
assert_eq!(percent_text(-0.0001), "0.0%", "never -0.0%");
assert_eq!(percent_text(0.0), "0.0%");
assert_eq!(percent_text(f64::NAN), "n/a");