Hi all i am doing a program to round the text entered in a text box . Sample inputs
Entered value output value
100 100.00
50 50.00
Like this i would like to format my text box value on textBox1_Leave event
I tried this but didn't work for me
private void textBox1_Leave(object sender, EventArgs e)
{
string str = string.Format(textBox1.Text, "##.00");
textBox1.Text = str;
}
Can any one help me
You'll need to convert that string to an number, then call Format. Also, you were using format incorrectly. You'll need to use a placeholder, like this
string str = String.Format("{0:F2}", Double.Parse(textBox1.Text));
textBox1.Text = str;
Naturally this will puke if you put in non-numeric input. To allow for this, you can do some basic validation
double d = 0;
textBox1.Text =
Double.TryParse(textBox1.Text, out d) ? String.Format("{0:F2}", d) : "0";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With