NumericTextBox rounding incorrectly

1 Answer 25 Views
NumericTextBox
Phillip
Top achievements
Rank 1
Phillip asked on 22 Nov 2021, 10:23 PM

The RadNumericTextBox is rounding incorrectly after a postback.

I am currently using 5 decimal places with the value of

0.00481487920209807

When the page loads it displays correctly

After I click the Postback button the display value changes

<div class="form-row">
            <asp:Button ID="btnCausePostback" Text="Postback" runat="server" OnClick="btnCausePostback_Click" />
            <telerik:RadNumericTextBox runat="server" ID="txtUnitCost" NumberFormat-DecimalDigits="5" Value="0.00481487920209807" /></div>


protected void btnCausePostback_Click(object sender, EventArgs e)
        {

        }

 

I am not sure what the round function is doing in this scenario?

I do have a work around which is setting

NumberFormat-AllowRounding="false"
And doing my own rounding using Math.Round to set the value properly.

Currently using Q2 2021 Telerik version

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 24 Nov 2021, 12:27 PM

Hello Phillip,

The faced behavior comes from the difference between the banker(which is the default) and away-from-zero rounding used in RadNumericBox (applied after the PostBack occurs):

        internal static double? Round (double? number, NumberFormatSettings config)
        {
            if (!config.AllowRounding)
                return number;
            
	        return Math.Round(number.Value, config.DecimalDigits, MidpointRounding.AwayFromZero);
        }

You can find detailed explanation on the matter here:

http://csharphelper.com/blog/2017/01/use-bankers-rounding-and-normal-rounding-in-c/

You can disable it either by setting the AllowRounding="false" (as found by you), so the default input rounding will be applied, or by setting KeepNotRoundedValue="true".

Regards,
Vessy
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.

Phillip
Top achievements
Rank 1
commented on 24 Nov 2021, 03:26 PM

It makes sense to where the value comes from using away-from-zero. But why would the value round differently before a postback compared to after it? This seems illogical? Would it use the same rounding before and after (away-from-zero or banker) but not both?
Vessy
Telerik team
commented on 29 Nov 2021, 05:58 PM

Hi Phillip, unfortunately the client- and server-side rounding allows different precision, hence the faced difference. Nevertheless, I will pass your feedback to our developers so they can check whether there is something that could be improved on this matter.

Tags
NumericTextBox
Asked by
Phillip
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or