From: Dominik Csapak <d.csapak@proxmox.com>
To: Christoph Heiss <c.heiss@proxmox.com>, yew-devel@lists.proxmox.com
Subject: Re: [RFC PATCH yew-widget-toolkit 1/2] widget: form: number: round floats to nearest step value
Date: Mon, 23 Mar 2026 09:51:46 +0100 [thread overview]
Message-ID: <57e99cba-a8c3-4ff4-ba49-87352877ad64@proxmox.com> (raw)
In-Reply-To: <20260319170432.1533393-2-c.heiss@proxmox.com>
my first instinct would have been to simply change the formatting to
round to some value of precision but you're right that it's just masking
the symptom.
but I think this patch is also missing some parts. I think
ideally we want to have a 'decimalPrecision' property that controls
the value also on setting/reading, change event etc.
(like it exists in ExtJS)
that should by default use a sensible value (like you did here
with 100_000 it can be just '5')
what do you think?
On 3/19/26 6:04 PM, Christoph Heiss wrote:
> E.g. previously, for an input like
>
> Number::new()
> .name("some-float")
> .min(0.)
> .step(0.1)
> .submit_empty(false)
> .value(0.2)
>
> and pressing the "range-up" button on the input would result in
> 0.30000000000000004 - which is rather undesirable.
>
> Fix it by multiplying, rounding and then dividing the number again by
> some fixed multiplier - in this case, 100_000 was chosen. Should cover
> all realistic cases, as it handles up to five fractional digits, which
> for UI purposes should (hopefully!) be plenty enough. And if really
> needed, could be increased.
>
> Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
> ---
> Marked RFC as it may not be necessarily the *best* solution, but by far
> the *simplest*. Happy about other solution suggestions, of course.
>
> I also considered other solutions like e.g. doing the rounding
> (implicitly) when formatting the number as string above with
> `dom::format_float()` - but that solution seemed more like papering over
> the symptoms than fixing the root cause.
>
> src/widget/form/number.rs | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/src/widget/form/number.rs b/src/widget/form/number.rs
> index e5c849c..6db7063 100644
> --- a/src/widget/form/number.rs
> +++ b/src/widget/form/number.rs
> @@ -68,10 +68,14 @@ impl NumberTypeInfo for f64 {
> crate::dom::format_float(*self)
> }
> fn step_up(&self, step: Option<Self>) -> Self {
> - self + step.unwrap_or(1.0)
> + // Do a little dance here to round to the nearest step value, by multiplying,
> + // rounding to the nearest integer and dividing again
> + ((self + step.unwrap_or(1.0)) * 1e5).round() / 1e5
> }
> fn step_down(&self, step: Option<Self>) -> Self {
> - self - step.unwrap_or(1.0)
> + // Do a little dance here to round to the nearest step value, by multiplying,
> + // rounding to the nearest integer and dividing again
> + ((self - step.unwrap_or(1.0)) * 1e5).round() / 1e5
> }
> fn clamp_value(&self, min: Option<Self>, max: Option<Self>) -> Self {
> self.clamp(min.unwrap_or(f64::MIN), max.unwrap_or(f64::MAX))
next prev parent reply other threads:[~2026-03-23 8:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-19 17:04 [PATCH yew-widget-toolkit 0/2] widget: form: number: fix float display and focus Christoph Heiss
2026-03-19 17:04 ` [RFC PATCH yew-widget-toolkit 1/2] widget: form: number: round floats to nearest step value Christoph Heiss
2026-03-23 8:51 ` Dominik Csapak [this message]
2026-03-25 12:09 ` Christoph Heiss
2026-03-19 17:04 ` [PATCH yew-widget-toolkit 2/2] widget: form: number: allow focusing the input on click Christoph Heiss
2026-03-23 8:54 ` Dominik Csapak
2026-03-23 10:43 ` Dietmar Maurer
2026-03-23 8:50 ` partially applied: [PATCH yew-widget-toolkit 0/2] widget: form: number: fix float display and focus Dominik Csapak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=57e99cba-a8c3-4ff4-ba49-87352877ad64@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=c.heiss@proxmox.com \
--cc=yew-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox