Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 45 / 68

Rounding numbers (floor, ceil, round)

This lesson covers rounding decimals in Julia, with the aim of choosing the right method for the purpose.

floor() rounds down, ceil() rounds up, and round() rounds to nearest. Picture shaving down, bumping up, and snapping to whichever is closer.

The sample code shows that similar-looking decimals give different results depending on the function: floor(3.7) is 3.0, ceil(3.2) is 4.0, and round(3.5) is 4.0.

A common early stumble is that round() is not simple round-half-up. Julia's round() defaults to banker's rounding, which snaps a half to the nearest even number, so the result may not be what you expected.

In professional work, the choice matters — rounding in a monetary calculation, aligning decimal places for display — because the result changes with it.

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

๐Ÿงช This site can't compile or run Julia 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)