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 1B1921FF140 for ; Fri, 27 Mar 2026 11:27:22 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 62CA344C3; Fri, 27 Mar 2026 11:27:45 +0100 (CET) From: Christoph Heiss To: yew-devel@lists.proxmox.com Subject: [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision Date: Fri, 27 Mar 2026 11:27:28 +0100 Message-ID: <20260327102731.490175-1-c.heiss@proxmox.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1774607208972 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.053 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. 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: N4YYSPMU7G2ATGAWM34ZRGI7YQB52DKW X-Message-ID-Hash: N4YYSPMU7G2ATGAWM34ZRGI7YQB52DKW X-MailFrom: c.heiss@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: 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 => {} -- 2.53.0