What happens to fractional values when casting from double to int? (beginner)

_agx_
_agx_ used Ask the Experts™
on
From what I can tell, the fractional values are truncated and the result is  Math.floor().

        double dx = 4968525.66;
        Console.WriteLine((int)dx); // 4968525
        Console.WriteLine(Math.Floor(dx)); // 4968525

Is that always true? Any difference for negative values?  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Essentially. Remember, though, for negative values floor returns the value going away from zero, not towards.

Per http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx :

When you convert from a double or float value to an integral type, the value is truncated. If the resulting integral value is outside the range of the destination value, the result depends on the overflow checking context. In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.
Most Valuable Expert 2015

Author

Commented:
>> for negative values floor returns the value going away from zero, not towards.

Ah, right. I'm forgetting basic math ... ;-)

Most Valuable Expert 2015

Author

Commented:
Just the answer I was looking for, thanks.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. Glad to help  = )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial