System.Convert.ToInt vs (int)

前端 未结 4 1651
猫巷女王i
猫巷女王i 2020-12-18 22:38

I noticed in another post, someone had done something like:

double d = 3.1415;
int i = Convert.ToInt32(Math.Floor(d));

Why did they use the

4条回答
  •  暖寄归人
    2020-12-18 23:22

    Casting to int is implicit truncation, not implicit flooring:

    double d = -3.14;
    int i = (int)d;
    // i == -3
    

    I choose Math.Floor or Math.Round to make my intentions more explicit.

提交回复
热议问题