Why Math.Ceiling returns double?

后端 未结 5 1743
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 15:52

In C# the method Math.Ceiling returns a double value. Why does it not return int?

5条回答
  •  隐瞒了意图╮
    2021-01-07 16:57

    Math.Ceiling can return either a double or a decimal, depending on the type passed in. In other words, the output type of the method matches the input type (quite sensibly).

    They could have added a third overload that takes an int and returns an int, but there wouldn't have been much point to this - the function would always just return its input.

    You seem to be assuming that the purpose of Math.Ceiling is to cast a floating-point value to an integer, but that's usually not how it's used.

提交回复
热议问题