Math.Floor VS投为整型在C# [英] Math.Floor vs cast to an integral type in C#

查看:168
本文介绍了Math.Floor VS投为整型在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何理由之一将preFER使用Math.Floor VS铸造为整型?

 双击NUM;
双楼= Math.Floor(NUM);
 

 双击NUM;
长楼=(长)民;
 

解决方案

有一个整体式,并使用Math.Floor铸造之间的一些差异:

  1. 当铸造整数类型,你最终会得到一个整数类型(很明显)。所以,如果你想保持数为双,使用地板更容易。
  2. 作为1结果,铸件将不能正确如果给定的数目过大而被重新由给定积分型psented $ P $工作(一个双可以重新present大得多号码比长)。
  3. 在楼舍入到负无穷大。铸造轮趋于零。

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

double num;
long floor = (long)num;

解决方案

There are some differences between casting to an integral type and using Math.Floor:

  1. When casting to an integral type, you'll end up with an integral type (obviously). So if you want to keep the number as a double, using Floor is easier.
  2. As a consequence of 1, casting will not work correctly if the given number is too large to be represented by the given integral type (a double can represent much larger numbers than a long).
  3. Floor rounds towards negative infinity. Casting rounds towards zero.

这篇关于Math.Floor VS投为整型在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆