JS - Math
Updated at 2014-02-01 01:25
This note contains simple math related "hacks".
// Check if number has decimal places.
var hasDecimals = function(number) {
return (number % 1 != 0);
}
console.log(hasDecimals(10)); // false
console.log(hasDecimals(10.0)); // false
console.log(hasDecimals(10.1)); // true
// Floor hack
// ~~ turns any value to integer while removing all decimals.
var a = 4.4;
var b = 4.6;
var c = "10.312";
var d = "Hello";
console.log(~~a); // 4
console.log(~~b); // 4
console.log(~~c); // 10
console.log(~~d); // 0