Export (0) Print
Expand All
8 out of 11 rated this helpful - Rate this topic

Math.Floor Method

Returns the largest whole number less than or equal to the specified number.

[Visual Basic]
Public Shared Function Floor( _
   ByVal d As Double _
) As Double
[C#]
public static double Floor(
 double d
);
[C++]
public: static double Floor(
 double d
);
[JScript]
public static function Floor(
   d : double
) : double;

Parameters

d
A number.

Return Value

The largest whole number less than or equal to d. If d is equal to NaN, NegativeInfinity, or PositiveInfinity, then that value is returned.

Remarks

The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding toward negative infinity.

Example

[Visual Basic] 
' This example demonstrates Math.Ceiling()
'                           Math.Floor()
Imports System

Class Sample
   Public Shared Sub Main()
      Dim value As Double
      Dim nl As String = Environment.NewLine
      
' Return the smallest whole number greater than or equal to the 
' specified number.
' Note that the results are affected by the precision of the floating point
' number, value.

      Console.WriteLine("{0}Ceiling:", nl)
      For value = 0.0 To 1.1 Step 0.1
         Console.WriteLine("Ceiling({0:f}) = {1}", value, Math.Ceiling(value))
      Next value

' Return the largest whole number less than or equal to the specified
' number.
' Note that the results are affected by the precision of the floating point
' number, value.

      Console.WriteLine("{0}Floor:", nl)
      For value = 2.1 To 0.9 Step -0.1
         Console.WriteLine("Floor({0:f}) = {1}", value, Math.Floor(value))
      Next value
   End Sub 'Main
End Class 'Sample 
'
'This example produces the following results:
'
'Ceiling:
'Ceiling(0.00) = 0
'Ceiling(0.10) = 1
'Ceiling(0.20) = 1
'Ceiling(0.30) = 1
'Ceiling(0.40) = 1
'Ceiling(0.50) = 1
'Ceiling(0.60) = 1
'Ceiling(0.70) = 1
'Ceiling(0.80) = 1
'Ceiling(0.90) = 1
'Ceiling(1.00) = 1
'Ceiling(1.10) = 2
'
'Floor:
'Floor(2.10) = 2
'Floor(2.00) = 2
'Floor(1.90) = 1
'Floor(1.80) = 1
'Floor(1.70) = 1
'Floor(1.60) = 1
'Floor(1.50) = 1
'Floor(1.40) = 1
'Floor(1.30) = 1
'Floor(1.20) = 1
'Floor(1.10) = 1
'Floor(1.00) = 0
'

[C#] 
// This example demonstrates Math.Ceiling()
//                           Math.Floor()
using System;

class Sample 
{
    public static void Main() 
    {
    double value;
    string nl = Environment.NewLine;

// Return the smallest whole number greater than or equal to the 
// specified number.
// Note that the results are affected by the precision of the floating point
// number, value.

    Console.WriteLine("{0}Ceiling:", nl);
    for (value = 0.0; value <= 1.1; value += 0.1)
      Console.WriteLine("Ceiling({0:f}) = {1}", value, Math.Ceiling(value));

// Return the largest whole number less than or equal to the specified
// number.
// Note that the results are affected by the precision of the floating point
// number, value.

    Console.WriteLine("{0}Floor:", nl);
    for (value = 2.1; value >= 0.9; value -= 0.1)
      Console.WriteLine("Floor({0:f}) = {1}", value, Math.Floor(value));
    }
}
/*
This example produces the following results:

Ceiling:
Ceiling(0.00) = 0
Ceiling(0.10) = 1
Ceiling(0.20) = 1
Ceiling(0.30) = 1
Ceiling(0.40) = 1
Ceiling(0.50) = 1
Ceiling(0.60) = 1
Ceiling(0.70) = 1
Ceiling(0.80) = 1
Ceiling(0.90) = 1
Ceiling(1.00) = 1
Ceiling(1.10) = 2

Floor:
Floor(2.10) = 2
Floor(2.00) = 2
Floor(1.90) = 1
Floor(1.80) = 1
Floor(1.70) = 1
Floor(1.60) = 1
Floor(1.50) = 1
Floor(1.40) = 1
Floor(1.30) = 1
Floor(1.20) = 1
Floor(1.10) = 1
Floor(1.00) = 0
*/

[C++] 
// This example demonstrates Math.Ceiling()
//                           Math.Floor()
#using <mscorlib.dll>
using namespace System;

int main() 
{
    double value;
    String* nl = Environment::NewLine;

// Return the smallest whole number greater than or equal to the 
// specified number.
// Note that the results are affected by the precision of the floating point
// number, value.

    Console::WriteLine(S"{0}Ceiling:", nl);
    for (value = 0.0; value <= 1.1; value += 0.1)
      Console::WriteLine(S"Ceiling({0:f}) = {1}", __box(value), __box(Math::Ceiling(value)));

// Return the largest whole number less than or equal to the specified
// number.
// Note that the results are affected by the precision of the floating point
// number, value.

    Console::WriteLine(S"{0}Floor:", nl);
    for (value = 2.1; value >= 0.9; value -= 0.1)
      Console::WriteLine(S"Floor({0:f}) = {1}", __box(value), __box(Math::Floor(value)));
}
/*
This example produces the following results:

Ceiling:
Ceiling(0.00) = 0
Ceiling(0.10) = 1
Ceiling(0.20) = 1
Ceiling(0.30) = 1
Ceiling(0.40) = 1
Ceiling(0.50) = 1
Ceiling(0.60) = 1
Ceiling(0.70) = 1
Ceiling(0.80) = 1
Ceiling(0.90) = 1
Ceiling(1.00) = 1
Ceiling(1.10) = 2

Floor:
Floor(2.10) = 2
Floor(2.00) = 2
Floor(1.90) = 1
Floor(1.80) = 1
Floor(1.70) = 1
Floor(1.60) = 1
Floor(1.50) = 1
Floor(1.40) = 1
Floor(1.30) = 1
Floor(1.20) = 1
Floor(1.10) = 1
Floor(1.00) = 0
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Math Class | Math Members | System Namespace | Round | Ceiling

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.