Ad space (banner)
๐ŸนGo Lessons
Lesson 45 / 68

Rounding Numbers (Floor, Ceil, Round)

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

math.Floor() rounds down, math.Ceil() rounds up, and math.Round() rounds to nearest. "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: math.Floor(3.7) gives 3, math.Ceil(3.2) gives 4, and math.Round(3.5) gives 4. Notice that all of these work with the float64 type.

A common stumbling block for beginners is wanting an integer result. The math functions return float64, so converting the type again is needed when you want a whole number.

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 Go 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)