Math.Floor vs cast to an integral type in C#

前端 未结 5 1105
清歌不尽
清歌不尽 2020-12-31 01:51

Are there any reasons one would prefer to use Math.Floor vs casting to an integral type?

double num;
double floor = Math.Floor(num);

OR

5条回答
  •  悲&欢浪女
    2020-12-31 02:23

    Math.Floor is a fundamentally different operation from truncation because it handles negative numbers differently. Math.Floor(-1.5) == -2.0, while (int)-1.5 == -1.

提交回复
热议问题