Skip to content

How to adjust the slider's decimals? #4670

Answered by YgorSouza
AurevoirXavier asked this question in Q&A
Discussion options

You must be logged in to vote

When a step is set, the slider does some arithmetic on the value after rounding it, which causes these rounding errors:

if let Some(max_decimals) = self.max_decimals {
value = emath::round_to_decimals(value, max_decimals);
}
if let Some(step) = self.step {
let start = *self.range.start();
value = start + ((value - start) / step).round() * step;
}

It is pretty easy to reproduce:

            ui.add(
                egui::Slider::new(&mut self.value, 0.0..=2.0)
                    .max_decimals(2)
                    .step_by(0.1),
            );
            ui.label(self.value.t…

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@AurevoirXavier
Comment options

Comment options

You must be logged in to vote
2 replies
@AurevoirXavier
Comment options

@AurevoirXavier
Comment options

Comment options

You must be logged in to vote
1 reply
@AurevoirXavier
Comment options

Comment options

You must be logged in to vote
1 reply
@AurevoirXavier
Comment options

Answer selected by AurevoirXavier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants