为什么Math.Floor(双人间)返回一个double类型的值? [英] Why does Math.Floor(Double) return a value of type Double?

查看:614
本文介绍了为什么Math.Floor(双人间)返回一个double类型的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个小数或双左侧整数值。对于例如:我需要从4.6获取值4。我试着用Math.Floor功能,但它返回一个双值,例如:它是从4.6回到4.0。 MSDN文档说,它返回一个整数值。我失去了一些东西在这里?还是有不同的方式来实现我在寻找什么呢?谢谢你。

I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation says that it returns an integer value. Am I missing something here? Or is there a different way to achieve what I'm looking for? Thank you.

推荐答案

的范围双比更广泛 INT 的范围或。考虑下面的代码:

The range of double is much wider than the range of int or long. Consider this code:

double d = 100000000000000000000d;
long x = Math.Floor(d); // Invalid in reality



整数是的范围长<外/ code> - ?所以你会期望发生

The integer is outside the range of long - so what would you expect to happen?

通常情况下,你知道的值将的实际的是的范围内 INT ,所以你投吧:

Typically you know that the value will actually be within the range of int or long, so you cast it:

double d = 1000.1234d;
int x = (int) Math.Floor(d);



但是对于投责任是开发商,而不是数学。地板本身。这本来是不必要的限制,使其只失败,所有值的范围长

but the onus for that cast is on the developer, not on Math.Floor itself. It would have been unnecessarily restrictive to make it just fail with an exception for all values outside the range of long.

这篇关于为什么Math.Floor(双人间)返回一个double类型的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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