From 5405811eaa2af3e84cc3423010488d8429657bb8 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 20 Jan 2023 17:49:23 -0330 Subject: round displacement when converting units --- src/main.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'src/main.rs') 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::().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::().unwrap(), - self.ui.displacement_unit, - unit, - ) - ); - self.ui.displacement_unit = unit; + self.ui.convert_displacement(unit); } } } -- cgit v1.2.3