RadNumericTextBox - Show 4 decimal digits ALWAYS & NOT ROUNDED OFF

23 posts, 1 answers
  1. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 21 Oct 2008 Link to this post

    Hi

    I have a RadNumbericTextBox in a grid item template, which binds to the data item correctly.

    My Question:
    How do I set it to display 4 decimal digits but in such a way that:

    1) it ALWAYS shows 4 decimal digits 
        so if my value is 5.50 it must show 5.5000

    2) it does NOT round off the value
        so for 5.501599 it shows 5.5015

    <telerik:RadNumericTextBox ID="radNumForexComp" runat="server" Culture="English (South Africa)" 
        DbValue='<%# Databinder.Eval(Container.DataItem, "Forex Comp") %>' InvalidStyleDuration="100" 
        LabelCssClass="radLabelCss_Web20" Skin="Telerik" Width="80px" style="TEXT-ALIGN: right" Font-Size="8pt">  
        <NumberFormat AllowRounding="False" DecimalDigits="4"/>  
        <FocusedStyle BorderColor="Red" BorderStyle="Double" /> 
        <HoveredStyle BorderColor="Red" BorderStyle="Double" /> 
    </telerik:RadNumericTextBox> 



  2. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 21 Oct 2008 Link to this post

    Hi

    I ended up tweaking the DataBind event handler of the RadGrid
    and formatted my RadNumericTextBox manually as follows:

    If (radNumForexComp.Value.HasValue) Then 
        Dim parts() As String = radNumForexComp.Value.ToString().Split(".")  
        If (parts.Length > 1) Then 
            If (parts(1).Length > 4) Then 
                radNumForexComp.Value = Double.Parse(parts(0) & "." & parts(1).Substring(0, 4))  
            End If 
        End If 
        radNumForexComp.NumberFormat.DecimalDigits = 4  
    End If 


    This worked :)
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 22 Oct 2008 Link to this post

    No, i must have been dreaming it is still not working !?!?!?

    When I have a value like 5.1200
    it shows up as 5.12

    I want it to show the 4 decimal digits including the trailing zeros is this not possible?

    These are my settings on the RadNumericInputBox .............

    <

    NumberFormat AllowRounding="false" DecimalDigits="4" DecimalSeparator="." GroupSizes="3" GroupSeparator="," NegativePattern="-n" PositivePattern="n"/>

     

     

  5. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 23 Oct 2008 Link to this post

    Hi Ian Coetzer,

    Please review the following code library which elaborates on how to set variable decimal digits for RadNumericTextBox. Test it on your side and verify if this helps.

    Regards,
    Maria Ilieva
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 27 Oct 2008 Link to this post

    Hi

    Can you please forward me some example of how to implement this where the RadNumericTextBox is within a RadGrid column?

    I have looked at the generic example but cannot figure out how/where to place the javascript, server side code when I have a RadGrid within an asp.net panel and have AjaxManager linked to those.

    Thanks. if I could only get the initial load (via server side code) to show 4 decimal digits regardless of the value then the problem will be solved.

    How can I do this?

    Thank you.
  7. Mishel
    Mishel avatar
    17 posts
    Member since:
    Jun 2008

    Posted 30 Oct 2008 Link to this post

    Hi Ian,

    I also tested this code library but it actually does not work corectly in my scenario.
    I have similar problem like yours and appropriate solution is to use the NumberFormat  "KeepNotRoundedValue" property for controlling this behaviour.

    I hope this is helpful.

    Mishel.
  8. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 02 Nov 2008 Link to this post

    Hi

    I'm still battling with this, I even tried to use the DbValue property to set the value like "0.1500" and see if that maintains the four decimals but it did not work consistently. do you have any other solution / ideas to how one can display 4 decimals even though the last four are zeros?

    Bye
  9. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 14 Nov 2008 Link to this post

    Hi

    I do not see such a property under numberformat.KeepNotRoundedValue???
  10. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 14 Nov 2008 Link to this post

    Hi

    This is how I have been trying to get this to work.

        Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
            Dim radNumericTextboxes As New Collection()  
     
            radNumericTextboxes.Add("radNumForexComp")  
            radNumericTextboxes.Add("radNumForexRaw")  
            radNumericTextboxes.Add("radNumPGM")  
     
            For Each item As Telerik.Web.UI.GridItem In RadGrid1.Items  
                For Each ctlName As String In radNumericTextboxes  
                    If (Not item.FindControl(ctlName) Is NothingThen 
                        With DirectCast(item.FindControl(ctlName), Telerik.Web.UI.RadNumericTextBox)  
                            .NumberFormat.DecimalDigits = 4  
                            .NumberFormat.AllowRounding = False 
     
                            .Culture = New System.Globalization.CultureInfo("en-us")  
     
                        End With 
                    End If 
                Next 
            Next 
        End Sub 
  11. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 14 Nov 2008 Link to this post

    Hello Ian Coetzer,


    I assume you just use an older version of RadControls for ASP.NET AJAX. Please, give the latest version (Q3 2008 ) a try and let us know if you need further help.

    Greetings,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  12. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 14 Nov 2008 Link to this post

    Hi

    I'm on this file version of Telerik.Web.UI.dll

    2008.1.415.20
  13. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 14 Nov 2008 Link to this post

    Hi Ian Coetzer,


    You can download the latest version (2008.3.1105) of RadCalendar for ASP.NET AJAX from your account. Step by step instructions which can guide you through the upgrade process can be found here.

    Best wishes,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  14. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 14 Nov 2008 Link to this post

    Hi

    How will that help me?

    I'm making use of the numeric textbox Telerik.Web.UI.RadNumericTextBox
    and want it to show 4 decimal digits ALWAYS,

    for example: 
     
    value                what i want shown in the numeric textbox control
    5.15                   5.1500
    5                        5.0000
    5.015                 5.0150
    5.0505               5.0505
    5.05050505       5.0505

    What version of Telerik.Web.UI.dll should I use to achieve the above outcome?

    Thanks

  15. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 17 Nov 2008 Link to this post

    Hi

    Still no luck, so I tried out the following code:
    But it did not work, it would now show 5 decimal digits!

                            Dim values() As String = .DbValue.ToString().Split(".")  
                            If (values.Length > 1) Then 
                                .DbValue = values(0) & "." & values(1).PadRight(4, "0") & "1" 
                            End If 


    for example 5.05 would be shown as 5.05001 (5 decimal digits!?!?)
    How is this possible? I'm setting the DecimalDigits property to 4!

     

        Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
            Dim radNumericTextboxes As New Collection()  
     
            radNumericTextboxes.Add("radNumForexComp")  
            radNumericTextboxes.Add("radNumForexRaw")  
            radNumericTextboxes.Add("radNumPGM")  
     
            For Each item As Telerik.Web.UI.GridItem In RadGrid1.Items  
                For Each ctlName As String In radNumericTextboxes  
                    If (Not item.FindControl(ctlName) Is NothingThen 
                        With DirectCast(item.FindControl(ctlName), Telerik.Web.UI.RadNumericTextBox)  
                            .NumberFormat.DecimalDigits = 4  
                            .NumberFormat.AllowRounding = False 
     
                            '  
                            'ensure that there are always 4 visible decimal digits.  
                            '  
                            '1) the code did not work, now 5 decimal digits are shown  
                            '2) when saving all values are saved (overwritten) in database not only thee changed ones  
                            '  
     
                            Dim values() As String = .DbValue.ToString().Split(".")  
                            If (values.Length > 1) Then 
                                .DbValue = values(0) & "." & values(1).PadRight(4, "0") & "1" 
                            End If 
     
                            .Culture = New System.Globalization.CultureInfo("en-us")  
                        End With 
                    End If 
                Next 
            Next 
        End Sub 

    I'm not seeming to get any resolution to this issue

     

  16. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 17 Nov 2008 Link to this post

    Hi Ian,


    Can you specify the steps to reproduce the erroneous behavior on this online demo of the product? I tried to do that but unfortunately to no avail. This can help us identify the source of the issue and provide a solution for it.

    Best wishes,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  17. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 17 Nov 2008 Link to this post

    Hi, ok I'll try because I'm starting to really get tired of this issue.

    1) I have a RadGrid
    2) within the RadGrid I have an item template for one of thee columns containing:

    <telerik:RadNumericTextBox ID="radNumForexRaw" runat="server" Culture="English (South Africa)" 
        DBvalue='<%# Databinder.Eval(Container.DataItem, "Forex Raw") %>' InvalidStyleDuration="100" Type="Number" 
        LabelCssClass="radLabelCss_Web20" Skin="Telerik" Width="80px" style="TEXT-ALIGN: right" Font-Size="8pt">    
        <NumberFormat AllowRounding="false" DecimalDigits="4" />                                                        
        <FocusedStyle BorderColor="Red" BorderStyle="Double" /> 
        <HoveredStyle BorderColor="Red" BorderStyle="Double" /> 
    </telerik:RadNumericTextBox> 

    3) When the page loads it binds the telerik rad grid to a DataTable
    4) The data table contains a field called "Forex Raw" which contains a String value from the database
    5) This 'string' value can be either a double value like 15.05 OR it can contain nothing, in which case nothing is displayed in the textbox not even a zero is displayed - this is correct as per user requirements
    6) When the ItemDataBound event on the rad grid fires I do the following ...
    Dim radNumForexComp As Telerik.Web.UI.RadNumericTextBox = e.Item.FindControl("radNumForexRaw")  
     
    radNumForexRaw.BackColor = e.Item.BackColor  
     
                If (Not radNumForexRaw.DbValue Is NothingThen 
                    radNumForexRaw.Type = Telerik.Web.UI.NumericType.Number  
                    radNumForexRaw.NumberFormat.DecimalDigits = 4  
                    radNumForexRaw.NumberFormat.AllowRounding = False 
                    radNumForexRaw.Value = radNumForexComp.DbValue  
                End If 

    7) Also during the Radgrid PreRender event I have the following code ...

        Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
            Dim radNumericTextboxes As New Collection()  
            Dim script As String = String.Empty  
     
            
            radNumericTextboxes.Add("radNumForexRaw")  
             
     
            For Each item As Telerik.Web.UI.GridItem In RadGrid1.Items  
                For Each ctlName As String In radNumericTextboxes  
                    If (Not item.FindControl(ctlName) Is NothingThen 
                        With DirectCast(item.FindControl(ctlName), Telerik.Web.UI.RadNumericTextBox)  
                            .NumberFormat.DecimalDigits = 4  
                            .NumberFormat.AllowRounding = False 
     
                            If (Not .DbValue Is NothingThen 
                                .Type = Telerik.Web.UI.NumericType.Number  
                                .Value = .DbValue  
                              
                            End If 
                        End With 
                    End If 
                Next 
            Next 
        End Sub 

    8) Result : the value in the textbox still shows 15.05 instead of the desired 15.0500
  18. Answer
    Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 18 Nov 2008 Link to this post

    Hello Ian Coetzer,


    In order to show 4 decimal digits, you have to set AllowRounding="false" and KeepNotRoundedValue="true"
    Please find attached a sample web application that demonstrates the needed approach.


    You can review the following help article:
    http://www.telerik.com/help/aspnet-ajax/input_radnumerictextboxformatting.html

    Kind regards,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  19. Ian Coetzer
    Ian Coetzer avatar
    65 posts
    Member since:
    Sep 2004

    Posted 19 Nov 2008 Link to this post

    Hi,
    Thank you Telerik, I managed to get this figured out, but you know what the most important problem was that I was making use of a rather old version of thee Telerik.Web.UI.dll !!!!!!

    I upgraded this Monday to 2008.2.826.20 and it is working, primarily because of the new property called: KeepNotRoundedValue.
  20. Ben Grossman
    Ben Grossman avatar
    12 posts
    Member since:
    Nov 2009

    Posted 13 Sep 2013 Link to this post

    I know this is an old forum but I'm having a very simlar problem.  I need my textbox to ALWAYS display 8 decimal digits, which it does on focus but it cuts off trailing zeros on blur.  Here is the markup for my textbox:
    <telerik:RadNumericTextBox ID="TextBox1"
    runat="server"
    OnTextChanged="TextBox1_TextChanged"
    NumberFormat-AllowRounding="false"
    NumberFormat-DecimalDigits="8"
    NumberFormat-KeepNotRoundedValue="true"
    NumberFormat-KeepTrailingZerosOnFocus="true"
    AutoPostBack="true"></telerik:RadNumericTextBox>

    Any help would be greatly appreciated.

    Thanks,
    Ben
  21. Ben Grossman
    Ben Grossman avatar
    12 posts
    Member since:
    Nov 2009

    Posted 13 Sep 2013 Link to this post

    I know this is an old forum but I'm having a very simlar problem.  I need my textbox to ALWAYS display 8 decimal digits, which it does on focus but it cuts off trailing zeros on blur.  Here is the markup for my textbox:
    <telerik:RadNumericTextBox ID="TextBox1"
    runat="server"
    OnTextChanged="TextBox1_TextChanged"
    NumberFormat-AllowRounding="false"
    NumberFormat-DecimalDigits="8"
    NumberFormat-KeepNotRoundedValue="true"
    NumberFormat-KeepTrailingZerosOnFocus="true"
    AutoPostBack="true"></telerik:RadNumericTextBox>

    Any help would be greatly appreciated.

    Thanks,
    Ben
  22. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 16 Sep 2013 Link to this post

    Hi Ben,

    Showing the trailing zeros on blur is possible(correct) only with AllowRounding=true.

    This design limitation of the RadNumericTextBox caused by the JavaScript itself.
    For an example lets have number 1000000.11

    (1000000.11).ToString() //will return "1000000.11"
    (1000000.11).toPrecision(30) //will returns "1000000.10999999998603016138077"

    You probably known that ToString rounds the number. So when rounding is enabled, the control can use .toString(), and then add zeroes to the end
    So in your case 1000000.11 will be displayed as 1000000.11000000 (with the 6 added zeroes at the end)
    However if rounding is not enabled, the same number will be trimmed from its full precision to
    "1000000.10999999" , and I can bet that your clients would not expect to get such number when they type 1000000.11.

    The code above is just to illustrate the problem, in the real code the things are more complex, but the principle remains the same. In the world of float the number never appears as you would expect unless you round it.

    Slightly off topic: In JavaScript when you execute
    (0.1 * 3).toString()
    (0.1 * 3).toString() you will get "0.30000000000000004", and I still hope that they will implement the Decimal type some day.

    Regards,
    Vasil
    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.
  23. Adam
    Adam avatar
    2 posts
    Member since:
    Oct 2013

    Posted 16 Oct 2013 Link to this post

    Here is how I solved this problem in my program:

    <telerik:RadNumericTextBox >
        <ClientEvents OnValueChanging="PreventRounding" />
        <NumberFormat AllowRounding="True" DecimalDigits="4"/>
    </telerik:RadNumericTextBox>
    function PreventRounding(sender, eventArgs) {
        var newVal = eventArgs.get_newValue();
        var nf = sender.get_numberFormat();
        var decimals = nf.DecimalDigits;
     
        eventArgs.set_newValue(truncateDecimals(newVal, decimals));
    }
     
    function truncateDecimals (num, digits) {
        var numS = num.toString();
        var decPos = numS.indexOf('.');
        var result = numS.substr(0, 1 + decPos + digits);
         
        if (decPos == -1) { //for no decimal point entered, no need to truncate
            return num;
        }
     
        if (isNaN(result)) { //for numbers between 0 and 1.
            result = 0;
        }
     
        return parseFloat(result);
    }

    Basically, setting AllowRounding to true shows trailing zeros, and I truncate any changed value by capturing the change via the OnValueChanging event.

    This may have a performance impact if you're doing this a lot, but it's the only way I've found to achieve the desired result.
  24. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 17 Oct 2013 Link to this post

    Hi Adam,

    Good approach if it fits your requirements, but keep in mind that it will not work for very small numbers like 0.000000123 regardless of the the DecimalDigits used.

    Regards,
    Vasil
    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017