In C#, why don’t the rounding math functions Floor, Ceiling and Round return an int? Considering the result of the function will always be an integer, why does it return a float, double or decimal?
In C#, why don’t the rounding math functions Floor, Ceiling and Round return an
Share
doublehas the range of ±5.0 × 10−324 to ±1.7 × 10308 andlonghas the range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Unfortunately not all integral floating point values can be represented by an integer.For example,
1e19has already exceeded the range of a 64-bit signed integer.While it is true that the single-argument overloads
Math.Round(double)andMath.Round(decimal)will always return an integral value, these overloads still cannot return an integer value type.If you know that the value passed to the function will return a value representable by an integer value type, you can cast it yourself. The library won’t do that because it needs to consider the general case.