Every time I use Math.Round/Floor/Ceiling
I always cast to int
(or perhaps long
if necessary). Why exactly do they return double
if it's always returning an integer.
round() is a built-in math function which returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result after adding 1/2, and casting the result to type long. If the argument is NaN, the result is 0.
Round(decimal) will always return an integral value, these overloads still cannot return an integer value type.
The Math. floor() method rounds a number DOWN to the nearest integer.
The cast rounds it "down" toward zero, while Math. floor rounds towards negative infinity.
The result might not fit into an int (or a long). The range of a double is much greater.
Approximate range of double: ±5.0 × 10−324 to ±1.7 × 10308
(Source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With