tags:

views:

1330

answers:

3

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
Thanks nickf! The last round method was unknown to me but is very usefull!
Patrick Peters
@nickf : Thats neat! +1
Codex
+2  A: 
double d = 1.234;
int i = Convert.ToInt32(d);

Reference

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
A: 
double d;
int rounded = (int)Math.Round(d);