public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: Christoph Heiss <c.heiss@proxmox.com>, yew-devel@lists.proxmox.com
Subject: Re: [PATCH yew-widget-toolkit v3] widget: form: number: round floats to some decimal precision
Date: Wed, 6 May 2026 23:19:44 +0200	[thread overview]
Message-ID: <64564f66-5688-41b8-897b-7481ceb2c74e@proxmox.com> (raw)
In-Reply-To: <20260505132650.1158715-1-c.heiss@proxmox.com>

Am 05.05.26 um 15:25 schrieb Christoph Heiss:
> The decimal precision is controllable through a property.
> 
> E.g. previously, for an input like
> 
>   Number::new()
>       .name("some-float")
>       .step(0.1)
>       .value(0.2)
> 
> and pressing the "range-up" button on the input would result in
> 0.30000000000000004 - which is rather undesirable.
> 
> Rounding is done on step up, step down, on creation, on the validated
> form value and on the parsed value for `on_input`, if any.

While this should address such rounding on stepping, a user entering
0.30000000000000004 explicitly and then submitting that would still result
in the not-rounded value getting submitted.

What about just rounding in the validator, which is basically the single
source of truth and might also make this change a bit shorter. I.e., add
decimal_places to the ValidateClosure generic struct.

Am 05.05.26 um 15:25 schrieb Christoph Heiss:
> @@ -79,6 +81,12 @@ impl NumberTypeInfo for f64 {
>      fn is_decimal() -> bool {
>          true
>      }
> +    fn round_to_precision(&self, precision: u8) -> Self {
> +        // Do a little dance here to round to the nearest step value, by multiplying,
> +        // rounding to the nearest integer and dividing again
> +        let m = 10f64.powf(precision as f64);

As precision is always an integer this should probably use [powi] over
powf, which should be also slightly faster, not that it matters much here.

[powi]: https://doc.rust-lang.org/std/primitive.f64.html#method.powi

> +        (self * m).round() / m
> +    }
>  }
>  
>  // Note: Error message from rust parse() are not gettext translated, so try to do all




      reply	other threads:[~2026-05-06 21:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-05 13:18 [PATCH yew-widget-toolkit v3] widget: form: number: round floats to some decimal precision Christoph Heiss
2026-05-06 21:19 ` Thomas Lamprecht [this message]

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=64564f66-5688-41b8-897b-7481ceb2c74e@proxmox.com \
    --to=t.lamprecht@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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal