Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a textbox. How can I make a textbox to accept only two digits after the decimal, although in the database, the value contains more than two digits after the decimal?

If I enter 5, it should take as 5.00.
Similarly:
for 5.1 it should take as 5.10
for 5.1234 it should take as 5.12

Hope you'll suggest me a solution.
Posted
Updated 8-Nov-10 22:59pm
v2

Example
double p = 123456.12345678;
            double p1 = Math.Round(p,2);
            MessageBox.Show(p1.ToString());


Please vote and Accept Answer if it Helped.
 
Share this answer
 
v2
Comments
[no name] 10-Nov-10 3:52am    
bad answer
shakil0304003 11-Nov-10 11:55am    
why?
shakil0304003 11-Nov-10 11:56am    
From whom, i copy it?
If you are asking for Windows forms apps.

Inside TextBox Leave Event

C#
private void textBox1_Leave(object sender, System.EventArgs e)
{
    TextBox1.Text = double.Parse(TextBox1.Text).ToString("f2");

}



Please vote and Accept Answer if it Helped.
 
Share this answer
 
v3
Comments
Eddy Vluggen 9-Nov-10 6:14am    
Will convert it to a string. Once he saves it to the database, it'll contain more than the specified two decimal numbers.
Hiren solanki 9-Nov-10 6:16am    
@Eddy : for that he need to manually setup decimal points in DB for particular column
If it's a decimal in the database, then it'll simply be a fractional number without any formatting applied. If you only want to see two decimals, then cut off all other decimals when retrieving the value and before displaying it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900