From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id AF02D1FF136 for ; Mon, 23 Mar 2026 09:52:09 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7B5FBDAD1; Mon, 23 Mar 2026 09:52:28 +0100 (CET) Message-ID: <57e99cba-a8c3-4ff4-ba49-87352877ad64@proxmox.com> Date: Mon, 23 Mar 2026 09:51:46 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Subject: Re: [RFC PATCH yew-widget-toolkit 1/2] widget: form: number: round floats to nearest step value To: Christoph Heiss , yew-devel@lists.proxmox.com References: <20260319170432.1533393-1-c.heiss@proxmox.com> <20260319170432.1533393-2-c.heiss@proxmox.com> Content-Language: en-US From: Dominik Csapak In-Reply-To: <20260319170432.1533393-2-c.heiss@proxmox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1774255869124 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.041 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: Q3PISFYRBKLXC3SFZ6ZAZS52TB6FXGI4 X-Message-ID-Hash: Q3PISFYRBKLXC3SFZ6ZAZS52TB6FXGI4 X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Yew framework devel list at Proxmox List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 > --- > 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 + 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 - 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, max: Option) -> Self { > self.clamp(min.unwrap_or(f64::MIN), max.unwrap_or(f64::MAX))