public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision
@ 2026-03-27 10:27 Christoph Heiss
  2026-04-07 13:58 ` Dominik Csapak
  0 siblings, 1 reply; 2+ messages in thread
From: Christoph Heiss @ 2026-03-27 10:27 UTC (permalink / raw)
  To: yew-devel

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





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision
  2026-03-27 10:27 [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision Christoph Heiss
@ 2026-04-07 13:58 ` Dominik Csapak
  0 siblings, 0 replies; 2+ messages in thread
From: Dominik Csapak @ 2026-04-07 13:58 UTC (permalink / raw)
  To: Christoph Heiss, yew-devel

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 <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 => {}





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-07 13:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-27 10:27 [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision Christoph Heiss
2026-04-07 13:58 ` Dominik Csapak

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