Marko,
In integer (longs) math, the fractional part is thrown away.
Math.Round() can round. But, see the posts in this newsgroup regarding
rounding. Math.Round() may not work the way you want it to. It follows IEEE
specifications.
If you want a fractional result, you must convert or use floating point
numbers (e.g. float, double, decimal) prior to multiplication or division.
Obviously addition and subtraction cannot introduce fractional parts.
When using constants like 23 and 40, you can append them with a letter to
tell the compiler their type. (f=float, d=double, m=decimal)
So, 23f is the float number 23.0, and 40d is the double 40.0.
Otherwise, as you suggest, you must cast (convert) to the appropriate
floating point representation prior to making the conversion.
Hope this helps...
Frisky
"Marko Becirevic" <ma*****************@zg.t-com.hr> wrote in message
news:da**********@ss405.t-com.hr...
I wrote a quick method to perform a calculation to determine the average
of two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using long data types, not double. So, how
does c# and/or .NET round numbers? Does is just truncate the value? If
rounded, I think 31.5 should have been rounded to 32.
(long) 23 + (long) 40 / (long) 2 = long
it always rounds to smaller number: 1.99 rounded is 1.
Cast first operand to double, do calculations, add 0.5 and then round:
(int) (((double) x + y) / 2 + 0.5)