Search code examples
c#floating-pointintfloor

c# equivalent of the c++ "floor" function


I'm looking for an implementation of floor(x) in C# so that it would match the C++ version.

floor of 2.3 is 2.0
floor of 3.8 is 3.0
floor of -2.3 is -3.0
floor of -3.8 is -4.0

Since a C++ reviewer will be checking my code, what implementation of floor is reasonable in C# so that he doesn't pull out his (last remaining) hairs?


Solution

  • You can use the Math class. There are many overloaded form of Math.Floor

    1. Math.Floor(Double)
    2. Math.Floor(Decimal)

    Simple Usage :

    Math.Floor(2.3) // == 2.0
    Math.Floor(3.8) // == 3.0
    Math.Floor(-2.3) // == -3.0
    Math.Floor(-3.8) // == -4.0