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)); } }