From: Dominik Csapak <d.csapak@proxmox.com>
To: Christoph Heiss <c.heiss@proxmox.com>, yew-devel@lists.proxmox.com
Subject: Re: [PATCH yew-widget-toolkit v2] widget: form: number: round floats to some decimal precision
Date: Tue, 7 Apr 2026 15:58:23 +0200 [thread overview]
Message-ID: <12d9ae9f-86f3-46e3-92ba-90eea536a0d4@proxmox.com> (raw)
In-Reply-To: <20260327102731.490175-1-c.heiss@proxmox.com>
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 => {}
prev parent reply other threads:[~2026-04-07 13:58 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-27 10:27 Christoph Heiss
2026-04-07 13:58 ` Dominik Csapak [this message]
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=12d9ae9f-86f3-46e3-92ba-90eea536a0d4@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox