From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id A812E1FF142 for ; Tue, 07 Apr 2026 15:58:23 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7658E1D075; Tue, 7 Apr 2026 15:58:58 +0200 (CEST) Message-ID: <12d9ae9f-86f3-46e3-92ba-90eea536a0d4@proxmox.com> Date: Tue, 7 Apr 2026 15:58:23 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Subject: Re: [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision To: Christoph Heiss , yew-devel@lists.proxmox.com References: <20260327102731.490175-1-c.heiss@proxmox.com> Content-Language: en-US From: Dominik Csapak In-Reply-To: <20260327102731.490175-1-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: 1775570238165 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.050 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: 5ZR2SAUGMKNBMGBDEFCPUBN2IGP7ELGZ X-Message-ID-Hash: 5ZR2SAUGMKNBMGBDEFCPUBN2IGP7ELGZ 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: this patch only changes the precision during step_up/down or am I missing something here? If we do have such a precision field/decimal property, it should have an effect also on read/write value, renderer etc. (like i wrote in my last message) e.g. if i would set a value of '3.00001' with 'decimal_places' set to '1', it would not change the current behavior? On 3/27/26 11:26 AM, Christoph Heiss wrote: > The precision is controllable through a property. > > 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. > > Signed-off-by: Christoph Heiss > --- > v1: https://lore.proxmox.com/yew-devel/20260319170432.1533393-1-c.heiss@proxmox.com/T/#u > > Changes v1 -> v2: > * added property to control precision, as suggested by Dominik > > src/widget/form/number.rs | 38 ++++++++++++++++++++++++++------------ > 1 file changed, 26 insertions(+), 12 deletions(-) > > diff --git a/src/widget/form/number.rs b/src/widget/form/number.rs > index 698dc85..13601f8 100644 > --- a/src/widget/form/number.rs > +++ b/src/widget/form/number.rs > @@ -37,8 +37,8 @@ pub trait NumberTypeInfo: > > fn format(&self) -> String; > > - fn step_down(&self, step: Option) -> Self; > - fn step_up(&self, step: Option) -> Self; > + fn step_down(&self, step: Option, precision: u8) -> Self; > + fn step_up(&self, step: Option, precision: u8) -> Self; > > fn clamp_value(&self, min: Option, max: Option) -> Self; > > @@ -67,11 +67,17 @@ impl NumberTypeInfo for f64 { > fn format(&self) -> String { > crate::dom::format_float(*self) > } > - fn step_up(&self, step: Option) -> Self { > - self + step.unwrap_or(1.0) > + fn step_up(&self, step: Option, 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); > + ((self + step.unwrap_or(1.0)) * m).round() / m > } > - fn step_down(&self, step: Option) -> Self { > - self - step.unwrap_or(1.0) > + fn step_down(&self, step: Option, 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); > + ((self - step.unwrap_or(1.0)) * m).round() / m > } > fn clamp_value(&self, min: Option, max: Option) -> Self { > self.clamp(min.unwrap_or(f64::MIN), max.unwrap_or(f64::MAX)) > @@ -137,7 +143,7 @@ macro_rules! signed_number_impl { > fn format(&self) -> String { > (*self).to_string() > } > - fn step_down(&self, step: Option) -> Self { > + fn step_down(&self, step: Option, _precision: u8) -> Self { > let step = step.unwrap_or(1); > if *self >= (<$T>::MIN + step) { > self - step > @@ -145,7 +151,7 @@ macro_rules! signed_number_impl { > *self > } > } > - fn step_up(&self, step: Option) -> Self { > + fn step_up(&self, step: Option, _precision: u8) -> Self { > let step = step.unwrap_or(1); > if *self <= (<$T>::MAX - step) { > self + step > @@ -216,7 +222,7 @@ macro_rules! unsigned_number_impl { > fn format(&self) -> String { > (*self).to_string() > } > - fn step_down(&self, step: Option) -> Self { > + fn step_down(&self, step: Option, _precision: u8) -> Self { > let step = step.unwrap_or(1); > if *self >= (<$T>::MIN + step) { > self - step > @@ -224,7 +230,7 @@ macro_rules! unsigned_number_impl { > *self > } > } > - fn step_up(&self, step: Option) -> Self { > + fn step_up(&self, step: Option, _precision: u8) -> Self { > let step = step.unwrap_or(1); > if *self <= (<$T>::MAX - step) { > self + step > @@ -317,6 +323,14 @@ pub struct Number { > #[prop_or_default] > pub step: Option, > > + /// Number of decimal places to round to in case of floating point numbers. > + /// Defaults to 5 decimal places, which should cover most most cases. > + /// > + /// Does nothing for integers. > + #[builder(IntoPropValue, into_prop_value)] > + #[prop_or(5)] > + pub decimal_places: u8, > + > /// Force value. > /// > /// To implement controlled components (for use without a FormContext). > @@ -562,7 +576,7 @@ impl ManagedField for NumberField { > let n = match (n, self.result.is_ok()) { > (None, true) => Some(T::default().clamp_value(props.min, props.max)), > (Some(n), _) => { > - let next = T::step_up(&n, props.step); > + let next = T::step_up(&n, props.step, props.decimal_places); > match props.max { > Some(max) if next <= max => {} > None => {} > @@ -582,7 +596,7 @@ impl ManagedField for NumberField { > let n = match (n, self.result.is_ok()) { > (None, true) => Some(T::default().clamp_value(props.min, props.max)), > (Some(n), _) => { > - let next = T::step_down(&n, props.step); > + let next = T::step_down(&n, props.step, props.decimal_places); > match props.min { > Some(min) if next >= min => {} > None => {}