I have 3 TextBox.
I input 123.45 into the TextBox1, input 123.22 into the TextBox2, then do a calculation (123.45 + 123.22)/2 = 123.335.
I want to display the result 123.34 instead of 123.335 in the TextBox3.
How to do that?
Appreciate for your help!
I input 123.45 into the TextBox1, input 123.22 into the TextBox2, then do a calculation (123.45 + 123.22)/2 = 123.335.
I want to display the result 123.34 instead of 123.335 in the TextBox3.
How to do that?
Appreciate for your help!
f117_b2Posted Jul 10, 2006, 6:44 PM
Thank both Solitaire and hasan maqbool very much.
To Solitaire: Your stuff solves my problems.
To hasan maqbool: I am running .Net Framework v1.1. So I think the thing MidpointRounding.AwayFromZero works only with v2.0, which I don't have it yet.
hasan maqboolPosted Jul 8, 2006, 9:49 AM
decimal
ans = 0; try{
ans =
Convert.ToDecimal(textBox2.Text) + Convert.ToDecimal(textBox3.Text);ans = ans / 2;
this.textBox1.Text = Convert.ToString(Math.Round(ans, 2,MidpointRounding.AwayFromZero));}
catch (System.FormatException){
MessageBox.Show("Format is not consistent");}
Try it.
SolitairePosted Jul 7, 2006, 10:36 PM
The function is using a form of banker's rounding. Instead of rounding up from .205 to .21, it is rounding to the nearest EVEN digit.
For example, if the number ends with either .215 or .225, it returns .22 for both of them.
You can fix this problem by adding .005 to the number before rounding:
dval = Math.Round(dval + 0.005, 2)
Now .205 will round to .21
f117_b2Posted Jul 7, 2006, 10:28 AM
In most cases, it works fine. Try this two numbers out, (10982.63 + 8453.78)/2 = 9718.205.
Math.Round (dVal, 2) returns 9718.2, not 9718.21.
Any ideas?????
Krishnaraj LPosted Jul 3, 2006, 10:26 AM
f117_b2Posted Jul 3, 2006, 10:21 AM
It works like charm.
Krishnaraj LPosted Jun 30, 2006, 9:19 AM
Try this code it should work.
decimal
dVal = Convert.ToDecimal (textBox1.Text) + Convert.ToDecimal (textBox2.Text);dVal = dVal / 2;
textBox3.Text = Convert.ToString ( Math.Round (dVal, 2) );