66
rated 0 times
[
72]
[
6]
/ answers: 1 / hits: 33727
/ 15 Years ago, sun, january 24, 2010, 12:00:00
I want to truncate a number in javascript, that means to cut away the decimal part:
trunc ( 2.6 ) == 2
trunc (-2.6 ) == -2
After heavy benchmarking my answer is:
function trunc (n) {
return ~~n;
}
// or
function trunc1 (n) {
return n | 0;
}
More From » bit-manipulation