Radnumerictextbox with variable decimal digits

6 posts, 0 answers
  1. Shefali
    Shefali avatar
    7 posts
    Member since:
    Jul 2013

    Posted 17 Jul 2013 Link to this post

    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
  2. Shefali
    Shefali avatar
    7 posts
    Member since:
    Jul 2013

    Posted 17 Jul 2013 Link to this post

    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>
  3. Eyup
    Admin
    Eyup avatar
    3731 posts

    Posted 22 Jul 2013 Link to this post

    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.
  4. Shefali
    Shefali avatar
    7 posts
    Member since:
    Jul 2013

    Posted 12 Aug 2013 Link to this post

    Hi Eyup,

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

    Regards,
    Shefali
  5. Matt
    Matt avatar
    33 posts
    Member since:
    May 2013

    Posted 24 Jan 2014 Link to this post

    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
  6. Eyup
    Admin
    Eyup avatar
    3731 posts

    Posted 29 Jan 2014 Link to this post

    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.
Back to Top