ID:1476314
 
Truncates a number's decimal component. Useful for converting numbers to integers.

proc/trunc(x)
return x && x / abs(x) * round(abs(x))


I wrote this because I saw somewhere on the forums that someone recommended round()(whose default format is floor()) to truncate a number. That's incorrect. The reasoning for this is simple: truncating rounds towards zero and round() rounds towards negative infinity.

round(-3.14159) // -4
trunc(-3.14159) // -3

// The only exception!
round(3.14159) // 3
trunc(3.14159) // 3




round(num,1) will round towards 0 in the case of negatives.

round(-3.14159,1) // -3
Oh, wow. If you think about it, that does make sense. Ah well, now people will have two options to choose from, with the latter being faster. :(
In response to Nadrew
Nadrew wrote:
round(num,1) will round towards 0 in the case of negatives.

> round(-3.14159,1) // -3
>


False

round(-3.8, 1) // -4
In response to Lugia319
It's only partially false. It'll round up if the first digit after the decimal point is >5 and it'll round down(towards zero) when <5.

Hence why 3.14149 became 3 when round()ed.
Partially false is like partially pregnant.

round(x, 1) is pretty much the rounding we're used to. Rounding towards the nearest whole number.
Lugia's right. Rounding towards zero is different from rounding to the nearest whole number, floor, and ceil.

Rounding towards zero would be sign(n) * floor(abs(n)), which is what Magnum showed.