Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 45 / 68

Rounding Numbers (Floor, Ceiling, Round)

In this lesson you will learn how to round numbers in C# with Floor, Ceiling, and Round, and pick the right one for the job. This is for anyone searching for "C# Math.Round how to use".

Math.Floor() rounds down, Math.Ceiling() rounds up, and Math.Round() rounds to nearest (to even by default). "Shave it down", "push it up", and "go to whichever is closer" are useful mental labels.

The sample code shows how similar-looking decimals give different results depending on the method: Math.Floor(3.7) gives 3, Math.Ceiling(3.2) gives 4, and Math.Round(3.5) gives 4.

A common stumbling block for beginners is the slightly surprising default behaviour of Math.Round(). For monetary calculations it is worth checking the specification, and the MidpointRounding enum lets you control the tie-breaking rule precisely.

In real-world development, the choice changes your results - handling fractions in monetary calculations, lining up decimal places for display - so pick deliberately.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)