how do you convert a double into a int (can remove the rounding)
+7
A:
Use Math.round()
, possibly in conjunction with MidpointRounding.AwayFromZero
eg:
Math.Round(1.2) ==> 1
Math.Round(1.5) ==> 2
Math.Round(2.5) ==> 2
Math.Round(2.5, MidpointRounding.AwayFromZero) ==> 3
nickf
2009-03-11 04:33:57
Thanks nickf! The last round method was unknown to me but is very usefull!
Patrick Peters
2009-03-11 07:00:13
@nickf : Thats neat! +1
Codex
2009-03-11 10:57:31
+2
A:
double d = 1.234;
int i = Convert.ToInt32(d);
Handles rounding like so: "rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6."
John Sheehan
2009-03-11 04:34:09