Using C#'s `Decimal.Floor()` Method: Rounding Decimal Numbers Down
Learn how to use C#'s `Decimal.Floor()` method to round decimal numbers down to the nearest integer towards negative infinity. This tutorial explains `Decimal.Floor()`'s functionality, provides examples, and demonstrates its application in various numerical operations.
Using C#'s `Decimal.Floor()` Method
The C# `Decimal.Floor()` method rounds a decimal number down to the nearest whole number (integer) towards negative infinity. This is a helpful function for various mathematical and data manipulation tasks.
Understanding `Decimal.Floor()`
The `Decimal.Floor()` method is a static method within the `Decimal` structure (part of the `System` namespace). It takes a decimal number as input and returns a decimal number representing the largest integer less than or equal to the input.
`Decimal.Floor()` Syntax
public static decimal Floor(decimal d);
The method takes a decimal number (`d`) as input and returns a decimal number.
Example 1: Rounding a Positive Decimal
decimal num = 15.98M;
decimal flooredNum = Decimal.Floor(num); // flooredNum will be 15
Console.WriteLine(flooredNum);
Example 2: Rounding a Negative Decimal
decimal num = -10.6M;
decimal flooredNum = Decimal.Floor(num); // flooredNum will be -11
Console.WriteLine(flooredNum);
Example 3: Rounding a Whole Number
decimal num = 5.00M;
decimal flooredNum = Decimal.Floor(num); // flooredNum will be 5
Console.WriteLine(flooredNum);