Just a quick survey. Do you guys typically round up (`Math.ceil()`

), round down (`Math.floor()`

), or round to the nearest integer (`Math.round()`

)? Or perhaps you use different methods in various situations. If so, when do you round up? When do you round down? Etc...

Depends on what I'm trying to do.

If I need integer operations (e.g.

`5 / 2 = 2`

), I use

`Math.floor()`

. If I need to display floating numbers, but don't want the number to have a bazillion decimal digits, I may use

`Math.round()`

instead.

They each have their use-case, though I don't remember when I ever needed

`Math.ceil()`

.

Why is rounding down faster than the others? Does it take less processing power or something (not that it would be noticeable)? Wouldn't rounding up be just as fast as rounding down?

Because of how modern computers handle fractional numbers (aka "float" and "double").

Floats (and doubles) are constructed as:

`2^x * 1.f`

(

`x`

being the exponent, and

`f`

being the fraction). This may look complicated, but simply put: one part represents the number, e.g. 12345, and the other part represents the position of the decimal point, e.g. 2. So using my example,

`0.12345 * 10^2 = 12.345`

. Obviously, the computer will use binary numbers instead (and instead of

`0.`

, it will use

`1.`

), but the basic principle is identical and just as simple.

This means that if you're trying to convert a number from fractional to integral, you simply take the first

`x`

digits of

`f`

(if

`x`

is positive) and add 1 to the left end of the number. If

`x`

is 0, the number is 1, and if

`x`

is negative, the number is 0.

If you wanted to round up, you'd need to

*additionally* look at the digits you don't take, and see if at least one of them is non-zero (and add 1 to the number if needed). This extra work is why rounding up is slower than rounding down.