RadNumericBox floating-point errors

1 Answer 86 Views
NumericBox
Maciej
Top achievements
Rank 1
Maciej asked on 06 Jun 2023, 02:51 PM

Hi,

If I create RadNumericBox like that:

<input:RadNumericBox ValueFormat="{}{0:f1}" Value="3.6"/>

than I'll see 3.6 displayed, as expected.

However, if I press up-arrow, and then down-arrow, then in edit mode, the control will display

3.5999999999999996

Is there any way to get rid of these floating-point errors? This controls seems to be useless now.

Thanks!
Maciek

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 09 Jun 2023, 02:14 PM

Hello Maciej,

This is a double rounding error that occurs when you try to substract 1 from 4.6. You can recreate it using the following code:

double result = 4.6d - 1d;

It occurs on up and down arrows press because of the way RadNumericBox works. Basically, the ValueFormat is applicable only when the control is in display mode. This happens when when the textbox element loses the focus. When you click on the textbox, the control gets the focus and enters edit mode. In that case it is more meaningful and easier to maintain to have the raw value to edit it. 

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Maciej
Top achievements
Rank 1
commented on 09 Jun 2023, 08:57 PM

Hello Martin,

 

In your answer, you gave a better explanation of what I described, but you didn't answer my question.

We want our users to be able to edit a value with a given precision, it's not acceptable to display values like 3.5999999999999996 when the expected precision is one digit after the floating point. I think that such a UX requirement is well understandable.

Thanks,
Maciek 

Martin Ivanov
Telerik team
commented on 14 Jun 2023, 12:52 PM

Hi Marciek,

The RadNumericBox was designed to allow editing the exact raw value. The ValueFormat will only mask the display value, but won't round the actual value. What you can consider is to try to round the value manually in the property setter of the view model property bound to Value (in case you are using data binding). 

In case you need to mask also the edit value, you may consider using another component, like the RadMaskedNumericInput for example. This will allow you to mask both the editing and the display values.

Tags
NumericBox
Asked by
Maciej
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or