all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
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	[thread overview]
Message-ID: <20260327102731.490175-1-c.heiss@proxmox.com> (raw)

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 <c.heiss@proxmox.com>
---
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>) -> Self;
-    fn step_up(&self, step: Option<Self>) -> Self;
+    fn step_down(&self, step: Option<Self>, precision: u8) -> Self;
+    fn step_up(&self, step: Option<Self>, precision: u8) -> Self;
 
     fn clamp_value(&self, min: Option<Self>, max: Option<Self>) -> 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 {
-        self + step.unwrap_or(1.0)
+    fn step_up(&self, step: Option<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);
+        ((self + step.unwrap_or(1.0)) * m).round() / m
     }
-    fn step_down(&self, step: Option<Self>) -> Self {
-        self - step.unwrap_or(1.0)
+    fn step_down(&self, step: Option<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);
+        ((self - step.unwrap_or(1.0)) * m).round() / m
     }
     fn clamp_value(&self, min: Option<Self>, max: Option<Self>) -> 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>) -> Self {
+            fn step_down(&self, step: Option<Self>, _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>) -> Self {
+            fn step_up(&self, step: Option<Self>, _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>) -> Self {
+            fn step_down(&self, step: Option<Self>, _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>) -> Self {
+            fn step_up(&self, step: Option<Self>, _precision: u8) -> Self {
                 let step = step.unwrap_or(1);
                 if *self <= (<$T>::MAX - step) {
                     self + step
@@ -317,6 +323,14 @@ pub struct Number<T: NumberTypeInfo> {
     #[prop_or_default]
     pub step: Option<T>,
 
+    /// 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<T: NumberTypeInfo> ManagedField for NumberField<T> {
                 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<T: NumberTypeInfo> ManagedField for NumberField<T> {
                 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





                 reply	other threads:[~2026-03-27 10:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260327102731.490175-1-c.heiss@proxmox.com \
    --to=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal