Travis Pessetto's answer along with mozey's trunc2
function were the only correct answers, considering how JavaScript represents very small or very large floating point numbers in scientific notation.
For example, parseInt(-2.2043642353916286e-15)
will not correctly parse that input. Instead of returning 0
it will return -2
.
This is the correct (and imho the least insane) way to do it:
function truncate(number)
{
return number > 0
? Math.floor(number)
: Math.ceil(number);
}