aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs36
1 files changed, 26 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs
index 4c6cf43..aabef0a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,12 @@ use iced::{
Element, Sandbox, Settings,
};
-use volute::volume::{self, CubicMetre};
+use volute::{
+ round,
+ volume::{self, CubicMetre},
+};
+
+const DECIMAL_DIGITS: i8 = 4;
pub fn main() -> iced::Result {
App::run(Settings::default())
@@ -33,6 +38,25 @@ impl App {
}
}
+impl UI {
+ fn convert_displacement(&mut self, unit: volume::Unit) {
+ if self.displacement.len() > 0 {
+ self.displacement = format!(
+ "{}",
+ round(
+ volume::convert(
+ self.displacement.parse::<f64>().unwrap(),
+ self.displacement_unit,
+ unit,
+ ),
+ DECIMAL_DIGITS,
+ ),
+ );
+ }
+ self.displacement_unit = unit;
+ }
+}
+
impl Sandbox for App {
type Message = Message;
@@ -50,15 +74,7 @@ impl Sandbox for App {
self.set_displacement(&displacement);
}
Message::DisplacementUnit(unit) => {
- self.ui.displacement = format!(
- "{:.2}",
- volume::convert(
- self.ui.displacement.parse::<f64>().unwrap(),
- self.ui.displacement_unit,
- unit,
- )
- );
- self.ui.displacement_unit = unit;
+ self.ui.convert_displacement(unit);
}
}
}