在我的Rust代码中,我有一个函数,它接受浮点数和精度作为参数。该函数应返回格式化的字符串。然而,我不确定如何使用动态精度进行格式化。
fn convert(val: f32, precision: i16) -> String {
format!("{:.2}", val) // The 2 should be replaced with precision
}
// convert(1.2345, 1) -> 1.2
// convert(1.2345, 2) -> 1.23