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:
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