EN
C# / .NET - Math.Floor() method example
0 points
The Math.Floor()
function returns an integer value that is smaller than or equal to the argument - the result of round down operation.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Floor( 5.0 )); // 5
8
9
Console.WriteLine( Math.Floor( 2.49 )); // 2
10
Console.WriteLine( Math.Floor( 2.50 )); // 2
11
Console.WriteLine( Math.Floor( 2.51 )); // 2
12
13
Console.WriteLine( Math.Floor( -2.49 )); // -3
14
Console.WriteLine( Math.Floor( -2.50 )); // -3
15
Console.WriteLine( Math.Floor( -2.51 )); // -3
16
17
Console.WriteLine( Math.Floor( 0.999 )); // 0
18
Console.WriteLine( Math.Floor( 1.001 )); // 1
19
Console.WriteLine( Math.Floor( -1.001 )); // -2
20
}
21
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static decimal Floor(decimal number) { ... } 7 public static double Floor(double number) { ... } 8 // ... 9 } 10 } |
Parameters | number - double or decimal value (primitive value). |
Result |
Rounded down If If If |
Description | Floor is a static method that takes only one parameter and returns a rounded down value. |
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double FloorPrecised(double number, int precision)
6
{
7
double power = Math.Pow(10, precision);
8
9
return Math.Floor(number * power) / power;
10
}
11
12
public static void Main(string[] args)
13
{
14
Console.WriteLine( FloorPrecised( 5 , 0 ) ); // 5
15
Console.WriteLine( FloorPrecised( 5.0 , 0 ) ); // 5
16
Console.WriteLine( FloorPrecised( .5, 0 ) ); // 0
17
18
Console.WriteLine( FloorPrecised( 1.1234, 0 ) ); // 1
19
Console.WriteLine( FloorPrecised( 1.1234, 1 ) ); // 1.1
20
Console.WriteLine( FloorPrecised( 1.1235, 2 ) ); // 1.12
21
Console.WriteLine( FloorPrecised( 1.1235, 3 ) ); // 1.123
22
23
Console.WriteLine( FloorPrecised( -1.1234, 0 ) ); // -2
24
Console.WriteLine( FloorPrecised( -1.1234, 1 ) ); // -1.2
25
Console.WriteLine( FloorPrecised( -1.1234, 2 ) ); // -1.13
26
Console.WriteLine( FloorPrecised( -1.1234, 3 ) ); // -1.124
27
28
Console.WriteLine( FloorPrecised( 1234, -1 ) ); // 1230
29
Console.WriteLine( FloorPrecised( 1234, -2 ) ); // 1200
30
Console.WriteLine( FloorPrecised( 1234, -3 ) ); // 1000
31
32
Console.WriteLine( FloorPrecised( 5_000.000_001, 0 ) ); // 5000
33
Console.WriteLine( FloorPrecised( 5_000.000_001, 6 ) ); // 5000.000001
34
Console.WriteLine( FloorPrecised( 5_000.000_001, -3 ) ); // 5000
35
}
36
}