TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Math.Floor Method

This C# tutorial shows how to use the Math.Floor method in the .NET Framework.

Math.Floor rounds down. It operates on types such as decimal or double.

It reduces the value to the nearest integer. The Floor method is straightforward, but useful, when it is called for in programming.

Math.Ceiling

Example. First, the Floor method is found in the base class library and is available in the Math type. It implements the mathematical floor function, which finds the largest integer "not greater" than the original number.

Next: This example shows the Floor method being used on doubles that would be rounded down or rounded up with the Math.Round method.

Double

C# program that uses Math.Floor

using System;

class Program
{
    static void Main()
    {
	//
	// Two values.
	//
	double value1 = 123.456;
	double value2 = 123.987;
	//
	// Take floors of these values.
	//
	double floor1 = Math.Floor(value1);
	double floor2 = Math.Floor(value2);

	//
	// Write first value and floor.
	//
	Console.WriteLine(value1);
	Console.WriteLine(floor1);
	//
	// Write second value and floor.
	//
	Console.WriteLine(value2);
	Console.WriteLine(floor2);
    }
}

Output

123.456
123     [floor]
123.987
123     [floor]

In the example, the two numbers 123.456 and 123.987 are rounded down to the nearest integer. This means that regardless of how close they are close to 124, they are rounded to 123.

Note: Floor can be useful when rounding numbers that are part of a larger representation of another number.

Discussion. The Math.Floor method when given a positive number will erase the digits after the decimal place. But when it receives a negative number, it will erase the digits and increase the number's negativity by 1.

So: Using Math.Floor on a negative number will still decrease the total number. This means it will always become smaller.

Next, the Math.Floor method can be used on the 128-bit decimal type. When you call Math.Floor with this data type, the decimal.Floor static method is immediately called into. It is sometimes clearer to directly use decimal.Floor.

DecimalStatic Method

Summary. We looked at the Math.Floor method in the base class library. This method is occasionally useful when you are trying to represent ratios and percentages and do not want the figures to add up to greater than 1 or 100%.

Tip: We can use Math.Floor on the double type or decimal type, which calls into the decimal.Floor method.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf