1

Possible Duplicate:
Force ASP.NET textbox to display currency with $ sign
In jQuery, what's the best way of formatting a number to 2 decimal places?

I've a textbox control on my ASP.NET page which accepts amount of Money. If it is entered as 500, I want to display it as 500.00.Using textchanged event I couldn't solve the problem in C#.Hope javascript will do it.Can anyone help me out by providing the required javascript or any other way?

Community
  • 1
  • 1
Sukanya
  • 1,041
  • 8
  • 21
  • 40

2 Answers2

1

For your text box put the AutoPostBack="true" and in the page load event write the following code part

        if (IsPostBack == true)
        {
            if (TextBox1.Text.Length > 0)
            {
                TextBox1.Text = TextBox1.Text + ".00";
            }
        }

EDIT:

If that's the case then write the code like below.

        if (IsPostBack) 
        {
            if (TextBox1.Text.Length > 0) 
            {
                if (TextBox1.Text.IndexOf('.') == -1)
                {
                    TextBox1.Text = TextBox1.Text + ".00";
                }
                else if (TextBox1.Text.EndsWith(".")) 
                {
                    TextBox1.Text = TextBox1.Text + "00"; 
                }
                else 
                {
                    for (int i = 0; i <= 9; i++) 
                    {
                        if (TextBox1.Text.EndsWith("." + i)) 
                        {
                            TextBox1.Text = TextBox1.Text + "0";
                        }
                    }
                }
            }
        } 
1

You can do it in 2 ways:

  1. Formatted string (eg .ToString("##.###")
  2. Using javascript and appending 0's
Ankit
  • 6,388
  • 8
  • 54
  • 79