Javatpoint Logo
Javatpoint Logo

Decimal.Floor() Method in C#

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:

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#.

Output:

Original Value is: 15.98
The Floored Value is: 15

Explanation:

  1. As the program's entry point, the code declares a class called Demo with a Main method.
  2. 98M is the initial value assigned to the decimal variable original_Value.
  3. The floor_Value variable holds the result of rounding original_Value to the nearest integer towards negative infinity using the "Decimal.Floor" method.
  4. Using "Console.WriteLine" and string interpolation, the original and floored values are displayed on the console.
  5. 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#.

Output:

Original Value is: -10.6
The Floored Value is: -11

Explanation:

  1. In this example, the code defines the Main method of a class called Demo, which represents the program's entry point.
  2. The initial value of a decimal variable called "original_Value" is -10.6M.
  3. The result is stored in the variable floor_Value. The "Decimal.Floor" method rounds down original_Value to the nearest integer towards negative infinity.
  4. With string interpolation, "Console.WriteLine" prints the original and floored values to the console.
  5. 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:

Output:

Original Value is: 5.00
The Floored Value is: 5

Explanation:

  1. As the program's starting point, the code constructs a class called Demo with a Main method.
  2. The value 00M, which represents a decimal number with two decimal places, is used to initialize a decimal variable called original_Value.
  3. The result is stored in the variable floor_Value. The "Decimal.Floor" method rounds down original_Value to the nearest integer towards negative infinity.
  4. 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."
  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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA