Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friend .....

i have textbox in which i enter like 50 or 60 and i want to show result like 50.00 as i leave textbox ....can any buddy help me what i have to do...


thanks in advanced

Lakhan
Posted

C#
string  valueString = string.Format("{0:#.00}", value);
 
Share this answer
 
Comments
Espen Harlinn 30-Dec-11 9:28am    
5'ed!
 
Share this answer
 
Comments
Espen Harlinn 30-Dec-11 9:28am    
5'ed!
Hello,
If you enter into textbox a number that belongs to the set of real numbers,
you should know that the value of the text box is still
simple text or string.Entered number as a string shuld be transfered to real number.
So you first need to know whether the user entered number.
For this I usually use TryParse method, afther that if user have
entered the number you should round it if it have more than
two decimal places Math.Round(Number,2),
and the last is to show it in the text box
as a plain text or a string in following format "60" -> "60.00",
by using Number.ToString("### ##0.00") witch transfers real number to simple string with shown string format and round it to two decimals:


C#
string Text = " ";
double Number = 0;
bool IsReal = false;

Text = textBox1.Text;
IsReal = double.TryParse(Text,out Number);
if (IsReal)
{
   Number = Math.Round(Number,2);
   textBox1.Text = Number.ToString("### ##0.00");
}
else
{
   MessageBox.Show("You did not entered the number !");
}


Using string format "### ##0.00" inside the Number.ToString(),
you get rounded number to two decimal places so you usually do not need to use
Math.Round(Number,2), and the thousands are separated by space :

2.30
123.56
1 236.30
100 001.65
etc.

Try reading help instructions for string formatting inside your's programming language.There is a lot more to learn.

Bevare the code submited with this solution only works for double type of variables...

All the best,
Perić Željko
 
Share this answer
 
v5
Comments
Espen Harlinn 30-Dec-11 9:29am    
Nice effort, my 5
How about this

C#
private void textBox1_Leave(object sender, EventArgs e)
        {
            string str = String.Format("{0:F2}", Double.Parse(textBox1.Text));
            textBox1.Text = str;
        }
 
Share this answer
 
Comments
[no name] 28-Dec-11 0:33am    
THANKS DORABABU743........
demouser743 28-Dec-11 2:56am    
Mark as an answer if you get solved...
Espen Harlinn 30-Dec-11 9:30am    
Fair enough - 5'ed!
Write Code at Leave event of text box.

TextBox.Text = (Math.Round(Convert.ToDecimal(TextBox.Text),2)).ToString();
 
Share this answer
 
Comments
Espen Harlinn 30-Dec-11 9:30am    
Fair enough - 5'ed!

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