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

RadNumericTextBox - Show 4 decimal digits ALWAYS & NOT ROUNDED OFF

22 Answers 1115 Views
Input
This is a migrated thread and some comments may be shown as answers.
Ian Coetzer
Top achievements
Rank 2
Ian Coetzer asked on 21 Oct 2008, 12:15 PM
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> 



22 Answers, 1 is accepted

Sort by
0
Ian Coetzer
Top achievements
Rank 2
answered on 21 Oct 2008, 01:17 PM
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 :)
0
Ian Coetzer
Top achievements
Rank 2
answered on 22 Oct 2008, 03:52 PM
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"/>

 

 

0
Maria Ilieva
Telerik team
answered on 23 Oct 2008, 10:36 AM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 27 Oct 2008, 07:25 AM
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.
0
Mishel
Top achievements
Rank 1
answered on 30 Oct 2008, 09:20 AM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 02 Nov 2008, 12:20 PM
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
0
Ian Coetzer
Top achievements
Rank 2
answered on 14 Nov 2008, 11:29 AM
Hi

I do not see such a property under numberformat.KeepNotRoundedValue???
0
Ian Coetzer
Top achievements
Rank 2
answered on 14 Nov 2008, 11:33 AM
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 
0
Missing User
answered on 14 Nov 2008, 11:36 AM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 14 Nov 2008, 11:47 AM
Hi

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

2008.1.415.20
0
Missing User
answered on 14 Nov 2008, 11:53 AM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 14 Nov 2008, 11:58 AM
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

0
Ian Coetzer
Top achievements
Rank 2
answered on 17 Nov 2008, 07:33 AM
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

 

0
Missing User
answered on 17 Nov 2008, 02:42 PM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 17 Nov 2008, 02:56 PM
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
0
Accepted
Missing User
answered on 18 Nov 2008, 11:34 AM
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.
0
Ian Coetzer
Top achievements
Rank 2
answered on 19 Nov 2008, 07:22 PM
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.
0
Ben Grossman
Top achievements
Rank 1
answered on 13 Sep 2013, 12:41 PM
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
0
Ben Grossman
Top achievements
Rank 1
answered on 13 Sep 2013, 12:42 PM
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
0
Vasil
Telerik team
answered on 16 Sep 2013, 02:56 PM
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.
0
Adam
Top achievements
Rank 1
answered on 16 Oct 2013, 05:24 PM
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.
0
Vasil
Telerik team
answered on 17 Oct 2013, 07:23 AM
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.
Tags
Input
Asked by
Ian Coetzer
Top achievements
Rank 2
Answers by
Ian Coetzer
Top achievements
Rank 2
Maria Ilieva
Telerik team
Mishel
Top achievements
Rank 1
Missing User
Ben Grossman
Top achievements
Rank 1
Vasil
Telerik team
Adam
Top achievements
Rank 1
Share this question
or