use super::funejson::Value; use super::math_round_float::round_float; use super::text_format_decimal::format_decimal; /// A fraction as a percentage with fixed places; the product is cleaned of /// binary noise at 12 places first, so 0.145 is "15%", not "14%". /// /// # Panics /// Panics if the value is not finite or `decimals` is outside 0..=12. pub fn format_percent(value: f64, decimals: i64) -> String { if !value.is_finite() { panic!("value must be a finite number, received {}", value); } let percent = round_float(value * 100.0, 12); format!("{}%", format_decimal(percent, decimals, false, "")) } pub fn fune_vector(args: &[Value]) -> Value { Value::str(&format_percent(args[0].as_f64(), args[1].as_i64())) }