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

前端 未结 5 1104
清歌不尽
清歌不尽 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:19

    It differs for negative values:

    double num = -1.3;
    double floor = Math.Floor(num); // = -2
    long cast = (long)num; // = -1
    

提交回复
热议问题