In this article, you will learn about the Decimal.Floor method in C# with its syntax, parameters, and examples.
What is the Decimal.Floor?
In C#, the Decimal.Floor method is a part of the Decimal struct and a member of the System namespace. This method may be used to truncate the decimal part of a decimal number by removing the fractional component or rounding the decimal number to the nearest integer.
The "Decimal.Floor" method is particularly useful when you need to work with integers or when you want to remove the fractional part of a decimal number.
The Decimal.Floor method rounding ensures that the result is less than or equal to the original number, compared to other rounding methods like Math.Round .
Syntax:
It has the following syntax:
public static decimal Floor(decimal d)
Access Modifier
public: The visibility of the method is specified by the access modifier public. In this case, the method may be accessed from any other code.
Static Keyword
static: Static indicates that the method is not an instance of the type. It is part of the type itself. In other words, you can call this method without creating an instance of the Decimal struct.
Return Type
decimal: The value that the method returns has a decimal data type. The Decimal.Floor returns a decimal value in this particular case.
Method Name
Floor: Floor is the name of the method. It indicates that the process rounds down a decimal number by performing the floor operation.
Parameter
decimal d: The Decimal before the parameter name "d" specifies the parameter's data type. In this case, the decimal number to be rounded down is the only parameter of type decimal that the method accepts.
Example 1:
Let us take an example to illustrate the use of Decimal.Floor method in C#.
using System;
class Demo
{
static void Main()
{
decimal original_Value = 15.98M;
// To round down to the next integer, use "Decimal.Floor".
decimal floor_Value = Decimal.Floor(original_Value);
//Displaying the original and floored values
Console.WriteLine($"Original Value is: {original_Value}");
Console.WriteLine($"The Floored Value is: {floor_Value}");
}
}
Output:
Original Value is: 15.98
The Floored Value is: 15
Explanation:
- As the program's entry point, the code declares a class called Demo with a Main method.
- 98M is the initial value assigned to the decimal variable original_Value.
- The floorValue variable holds the result of rounding originalValue to the nearest integer towards negative infinity using the "Decimal.Floor" method.
- Using "Console.WriteLine" and string interpolation, the original and floored values are displayed on the console.
- The program's output, "Original Value is: 15.98" and "Floored Value is: 15", which demonstrates how the decimal number gets rounded down to the closest integer when executed.
Example 2:
Let us take another example to illustrate the use of Decimal.Floor method in C#.
using System;
class Demo
{
static void Main()
{
decimal original_Value = -10.6M;
//To round down to the next integer, use "Decimal.Floor".
decimal floor_Value = Decimal.Floor(original_Value);
// Displaying the original and floored values
Console.WriteLine($"Original Value is: {original_Value}");
Console.WriteLine($"The Floored Value is: {floor_Value}");
}
}
Output:
Original Value is: -10.6
The Floored Value is: -11
Explanation:
- In this example, the code defines the Main method of a class called Demo , which represents the program's entry point.
- The initial value of a decimal variable called "original_Value" is -10.6M .
- The result is stored in the variable floorValue. The "Decimal.Floor" method rounds down originalValue to the nearest integer towards negative infinity.
- With string interpolation, "Console.WriteLine" prints the original and floored values to the console.
- The program's output, "Original Value is: -10.6" and "Floored Value is: -11", which demonstrates how to round a negative decimal number down to the closest integer when executed.
Example 3:
using System;
class Demo
{
static void Main()
{
decimal original_Value = 5.00M;
// To round down to the next integer, use "Decimal.Floor".
decimal floor_Value = Decimal.Floor(original_Value);
// Displaying the original and floored values
Console.WriteLine($"Original Value is: {original_Value}");
Console.WriteLine($"The Floored Value is: {floor_Value}");
}
}
Output:
Original Value is: 5.00
The Floored Value is: 5
Explanation:
- As the program's starting point, the code constructs a class called Demo with a Main method.
- The value 00M , which represents a decimal number with two decimal places, is used to initialize a decimal variable called original_Value.
- The result is stored in the variable floorValue. The "Decimal.Floor" method rounds down originalValue to the nearest integer towards negative infinity.
- Using "Console.WriteLine" and string interpolation, the original and floored values are displayed to the console, displaying "Original Value is: 5.00" and "Floored Value is: 5."
- The result shows that Floor efficiently rounds the original number to the closest integer, resulting in "Floored Value" being represented as only "5" even though the original value included two decimal places.