Skip to content

web search done right

[calculator-widget] round numbers to 2 digits if they have more

Mikkel Denker09f240ba5421
diff --git a/core/src/widgets/calculator.rs b/core/src/widgets/calculator.rs
index 3e99b6e3..5d260032 100644
--- a/core/src/widgets/calculator.rs
+++ b/core/src/widgets/calculator.rs
@@ -191,9 +191,25 @@ impl Calculator {
return Err(Error::CalculatorParse);
}
+ let result: String = res
+ .get_main_result_spans()
+ .map(|span| match span.kind() {
+ fend_core::SpanKind::Number => {
+ let num = span.string().parse::<f64>().unwrap();
+
+ if num.fract() > 0.01 {
+ format!("{:.2}", num)
+ } else {
+ num.to_string()
+ }
+ }
+ _ => span.string().to_string(),
+ })
+ .collect();
+
Ok(Calculation {
input: expr.to_string(),
- result: res.get_main_result().to_string(),
+ result,
})
}
}