From 2162ffff42c7cfda22543c0a4e6648f52a61a433 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 10 Jun 2020 16:22:31 +0200 Subject: [PATCH] [slider] round so what you see is what you get --- egui/src/math.rs | 7 +++++++ egui/src/widgets/slider.rs | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/egui/src/math.rs b/egui/src/math.rs index f6a25c23..ebc7090b 100644 --- a/egui/src/math.rs +++ b/egui/src/math.rs @@ -591,3 +591,10 @@ pub fn ease_in_ease_out(t: f32) -> f32 { } pub const TAU: f32 = 2.0 * std::f32::consts::PI; + +pub fn round_to_precision(value: f32, precision: usize) -> f32 { + // This is a stupid way of doing this, but stupid works. + format!("{:.*}", precision, value) + .parse() + .unwrap_or_else(|_| value) +} diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 026aaaa0..d848086d 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -91,9 +91,7 @@ impl<'a> Slider<'a> { } fn set_value_f32(&mut self, mut value: f32) { - if self.precision == 0 { - value = value.round(); - } + value = round_to_precision(value, self.precision); (self.get_set_value)(Some(value)); } }