diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-01-20 17:49:23 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-01-20 17:49:23 -0330 |
| commit | 5405811eaa2af3e84cc3423010488d8429657bb8 (patch) | |
| tree | d74df7e2c498e0db5b2b045444656026e14488a8 /src/main.rs | |
| parent | 4efbba96bbdf3dddeb0a853310b08a5281d2f8a0 (diff) | |
| download | volute-5405811eaa2af3e84cc3423010488d8429657bb8.zip | |
round displacement when converting units
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 36 |
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); } } } |