Last post Oct 17, 2011 09:26 PM by tima99
Oct 13, 2011 04:44 PM|tima99|LINK
I am new to asp.net development. I have a textbox that i would like the user to enter any number, including a number with a decimal and to display it like this:
User enters: 2900 display--> 29.00
User enters 34.7 display --> 34.70
User enters .347 display --> 0.34
User enters 100 display --> 1.00
I thought about dividing by 100 but I want the user to be able to enter in a decimal and display the number to the hundreths place. I basicallly always want to display the "cents"
I have no idea where to put the code.
<asp:TextBox id="txtCharge" tabIndex=12 runat="server" onkeydown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
Can someone give me some direction please?
Oct 13, 2011 04:47 PM|markfitzme|LINK
Take a look at the AjaxControl Toolkit. There is a MaskedEdit box that would make this easier for you because you could create a mask for your input, then just remove the additional mask formatting on postback and store it as a float, double, decimal, etc..
Oct 13, 2011 05:54 PM|tima99|LINK
I tried searching for this in our application and I dont think we use AJAX :( I tried to do something like this:
Protected Sub txtCharges_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCharges.TextChanged Dim txtContent As String Dim txtFormat As Double Dim txtNumber As Integer txtCharges.Text.Replace(".", "") txtContent = txtCharges.Text txtNumber = Convert.ToInt32(txtContent) txtFormat = (txtNumber / 100) txtCharges.Text = txtFormat.ToString("f2") End Sub
But this didnt even work? I entered 900 and didnt get any change. I have been looking at this all day and it just seems so simple to do but its not working for me. Thanks for any help guys
Oct 13, 2011 07:43 PM|lspence|LINK
Try starting with this sample code.
Protected Sub Textbox1_TextChanged(sender As Object, e As EventArgs)
Dim value As Decimal
If Decimal.TryParse(Textbox1.Text, value) Then
If Textbox1.Text.Contains(".") Then
Textbox1.Text = value.ToString("F")
If value.ToString("F").Length < 6 Then
Textbox1.Text = Convert.ToDecimal(value.ToString("F").Remove(value.ToString("F").IndexOf("."))).ToString("F")
Textbox1.Text = Convert.ToDecimal(value.ToString("F").Remove(value.ToString("F").IndexOf(".") - 2)).ToString("F")
Oct 17, 2011 09:26 PM|tima99|LINK
wonderful...i got it to work using similar logic!! thanks!