Within this guide, you will explore the Decimal.Floor function in C# along with its syntax, arguments, and illustrations.
What is the Decimal.Floor?
In C#, the Decimal.Floor function is a member of the Decimal structure within the System namespace. It serves the purpose of either discarding the decimal portion of a decimal number, eliminating the fractional part, or rounding the decimal number to the closest integer value.
The "Decimal.Floor" function is especially handy when dealing with whole numbers or when there is a need to eliminate the decimal portion of a decimal value.
The Decimal.Floor function guarantees that the resulting value is equal to or less than the initial number, distinguishing it from alternative techniques such as Math.Round.
Syntax:
It has the following syntax:
public static decimal Floor(decimal d)
Access Modifier
The access modifier "public" specifies the visibility of the method, allowing it to be accessed from any other code.
Static Keyword
Static: The term "static" signifies that the method is inherent to the type and not tied to any specific instance. Essentially, you have the ability to invoke this method directly without the need to instantiate the Decimal struct.
Return Type
decimal: The data type of the returned value is decimal. In this instance, Decimal.Floor will yield a decimal value.
Method Name
Floor: Floor represents the method name that signifies rounding down a decimal number through the floor operation.
Parameter
decimal valueToRound: The term "Decimal" preceding the parameter identifier "valueToRound" indicates the data type of the parameter. In this scenario, the method exclusively accepts a single parameter of type decimal, which is the decimal number intended for rounding down.
Example 1:
Let's consider a scenario to demonstrate the application of the Decimal.Floor method within 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's consider another instance to demonstrate the application of the 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.