Double Truncate

Hello everyone,

Yesterday, my friend was dealing with double variables in her cute method in .NET.
What she did not know was the difference between casting and conversion in .NET Framework.

Alright simple question;

double E1 = 1.24;
int noOfErroneousNodes = 3;
int a = (int)(noOfErroneousNodes * E1)
int b = Convert.ToInt32((noOfErroneousNodes * E1))

What is b and what is a?


Are not these the same, no they are not.
a = 3
b = 4

Actually to me b is more meaningful because double truncates into the nearest integer.
Whereas in a, tail part is always thrown out. Compiler does not care about the part after ".".

Therefore from now on, you know that Convert function and cast are different while using double to int truncates.

Happy coding...


Popular posts from this blog

Space Character Problem on IE 6, 7, and 8

Does Netflix work on iOS 5 Beta 4?

AWS encryption chart (SSE-S3 vs SSE-KMS vs SSE-C)