Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.Floor() Method in C#
The Decimal.Floor() method in C# rounds a specified decimal number down to the nearest integer toward negative infinity. This means it always rounds down, even for negative numbers.
Syntax
Following is the syntax −
public static decimal Floor(decimal val);
Parameters
-
val − The decimal value to round down to the nearest integer.
Return Value
Returns a decimal value representing the largest integer less than or equal to the specified decimal number.
Using Decimal.Floor() with Positive Numbers
Example
using System;
public class Demo {
public static void Main() {
decimal val1 = 25.25m;
decimal val2 = 11.85m;
decimal val3 = 7.99m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("Floor (val1) = " + Decimal.Floor(val1));
Console.WriteLine("Floor (val2) = " + Decimal.Floor(val2));
Console.WriteLine("Floor (val3) = " + Decimal.Floor(val3));
}
}
The output of the above code is −
Decimal 1 = 25.25 Decimal 2 = 11.85 Decimal 3 = 7.99 Floor (val1) = 25 Floor (val2) = 11 Floor (val3) = 7
Using Decimal.Floor() with Negative Numbers
Example
using System;
public class Demo {
public static void Main() {
decimal val1 = -25.25m;
decimal val2 = -11.85m;
decimal val3 = -0.5m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("Floor (val1) = " + Decimal.Floor(val1));
Console.WriteLine("Floor (val2) = " + Decimal.Floor(val2));
Console.WriteLine("Floor (val3) = " + Decimal.Floor(val3));
}
}
The output of the above code is −
Decimal 1 = -25.25 Decimal 2 = -11.85 Decimal 3 = -0.5 Floor (val1) = -26 Floor (val2) = -12 Floor (val3) = -1
Comparison with Math.Truncate()
| Method | Positive Numbers | Negative Numbers |
|---|---|---|
Decimal.Floor() |
Rounds down (25.8 ? 25) | Rounds toward negative infinity (-25.8 ? -26) |
Math.Truncate() |
Removes decimal part (25.8 ? 25) | Removes decimal part (-25.8 ? -25) |
Example Demonstrating the Difference
using System;
public class Demo {
public static void Main() {
decimal val = -3.7m;
Console.WriteLine("Original value: " + val);
Console.WriteLine("Floor: " + Decimal.Floor(val));
Console.WriteLine("Truncate: " + Math.Truncate(val));
}
}
The output of the above code is −
Original value: -3.7 Floor: -4 Truncate: -3
Conclusion
The Decimal.Floor() method always rounds down toward negative infinity, making it useful for financial calculations where you need consistent downward rounding. For negative numbers, this means rounding to a more negative value, which differs from simple truncation.
