This is a migrated thread and some comments may be shown as answers.

Radnumerictextbox with variable decimal digits

7 Answers 336 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Shefali
Top achievements
Rank 1
Shefali asked on 17 Jul 2013, 08:06 AM
Hi,

I need a radnumerictextbox with variable decimal digits, same as described in following code sample of yours -

http://www.telerik.com/community/code-library/aspnet-ajax/input/radnumerictextbox-with-variable-decimal-digits-not-rounded.aspx

This code sample is no longer working with  new version of radnumerictextbox. So please provide me the updated solution for this problem.

I am using version 2012.3.1127.40 of telerik dlls.

Please let me know if more information is required for this.

Thanks,
Shefali Gupta

7 Answers, 1 is accepted

Sort by
0
Shefali
Top achievements
Rank 1
answered on 17 Jul 2013, 08:08 AM
This is the code I am using ---


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="DefaultCS" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="radi" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <radi:RadScriptManager ID="ScriptManagerControl" runat="server" EnableScriptCombine="true" />
          
  
            <script type="text/javascript">
            // The provided solution works for AutoPostBack as well as for client-side NumericTextBox
            //
            // Note: you cannot use the regular OnBlur client-side event of the numeric textbox
            // as it is fired too late (after the value is already rounded)
            // basically the idea is to check whether the entered number contains more decimal digits than the number format expects
            // and that would be rounded. If that is the case we increase the NumberFormat.DecimalDigits count on-the-fly.
            window.onload = function()
            {
                var numericTextBox = <%= RadNumericTextBox2.ClientID %>;
                var oldTextBoxBlurHandler = numericTextBox.TextBoxBlurHandler;
                numericTextBox.TextBoxBlurHandler = function(e)
                {
                    var currentValue = numericTextBox.GetTextBoxValue();
                    var decimalPartStart = currentValue.indexOf(numericTextBox.NumberFormat.DecimalSeparator);
                      
                    if (decimalPartStart != -1)
                    {
                        var decimalPart = currentValue.substring(decimalPartStart + 1);
                        decimalPart = decimalPart.replace(/0+$/, "");
                        alert('Hi');
                        var decimalDigits = parseInt(decimalPart.length, 10);
                        if (decimalDigits >= defaultDecimalDigits)
                        {
                            numericTextBox.NumberFormat.DecimalDigits = decimalDigits;
                        }
                        else
                        {
                            numericTextBox.NumberFormat.DecimalDigits = defaultDecimalDigits;
                        
                    }
                    else
                    {
                        numericTextBox.NumberFormat.DecimalDigits = defaultDecimalDigits;
                    }
                      
                    oldTextBoxBlurHandler.call(this, e);
                }
            }
            </script>
            <div>
            <radi:RadNumericTextBox ID="RadNumericTextBox2" runat="server" Type="Number">
            </radi:RadNumericTextBox>
        </div>
    </form>
</body>
</html>
0
Eyup
Telerik team
answered on 22 Jul 2013, 08:03 AM
Hello Shefali,

You can use the following approach:
<telerik:RadNumericTextBox ID="RadNumericTextBox2" runat="server">
    <NumberFormat AllowRounding="false" KeepNotRoundedValue="true" />
    <ClientEvents OnValueChanged="setDisplayValue" OnLoad="setDisplayValue" />
</telerik:RadNumericTextBox>
JavaScript:
function setDisplayValue(sender, args) {
    sender.set_displayValue(sender.get_value());
}

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Shefali
Top achievements
Rank 1
answered on 12 Aug 2013, 10:15 AM
Hi Eyup,

The solution you provided is working for me. Thanks a lot!!!

Regards,
Shefali
0
Matt
Top achievements
Rank 1
answered on 24 Jan 2014, 06:40 PM
This solution is working for me, but seems to be a lot of work for something so commonly used.  Are there any plans for an enhancement in a future version to add an attribute to the RadNumericTextbox to allow variable decimal digits?

Thank you!

Matt
0
Eyup
Telerik team
answered on 29 Jan 2014, 11:44 AM
Hi Matt,

I'm afraid not. Basically, there is no point to have more than 16 decimal digits in the input because on the server it will be cast to double, which have limited precision. Nevertheless, you can use an alternative solution without javascript:
<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server">
    <NumberFormat AllowRounding="false" KeepNotRoundedValue="true" DecimalDigits="99" />
</telerik:RadNumericTextBox>

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 21 Aug 2020, 01:07 PM
Stumbled across this.  Does the setting DecimalDigits="99"  mean that is the maximum that MAY be shown or the maximum that ALWAYS WILL be there?  Thank you.
0
Eyup
Telerik team
answered on 26 Aug 2020, 05:11 AM

Hello Allen,

 

Although the DecimalDigits property can be set to a higher number just because of its type, realistically it can only operate with up to 16 digits:

Rounding digits must be between 0 and 15, inclusive.

This is due to inner specifics of the control.

There is a resource you can check for a possible workaround:
https://www.telerik.com/support/code-library/radnumerictextbox-with-variable-decimal-digits-not-rounded

But frankly said, it is too custom and quite old now so any modifications over it are beyond our support scope.

 

Regards,
Eyup
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Shefali
Top achievements
Rank 1
Answers by
Shefali
Top achievements
Rank 1
Eyup
Telerik team
Matt
Top achievements
Rank 1
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or