Skip to content

Instantly share code, notes, and snippets.

@isRuslan
Created May 24, 2015 20:40
Show Gist options
  • Select an option

  • Save isRuslan/5b4f8543a949cad6eb1b to your computer and use it in GitHub Desktop.

Select an option

Save isRuslan/5b4f8543a949cad6eb1b to your computer and use it in GitHub Desktop.
JS: numbers
var x = 3e2; // 300
var y = 3e-2; // 0.03
// only NaN dosen't equal to itself
function isNaN (number) {
return number !== number;
}
// string -> number
// 1. isNaN - return false if string is not a NaN => it is a number
isNaN('123'); // false
isNaN(null); // false too :(
// 2. better isNumber
function isNumber (string) {
// parseFloat - check "true/false/null/"
// isInfinite - check "Infinite/-Infinite/NaN"
return !isNaN(parseFloat(string)) && isInfinite(string);
}
// round numbers
Math.round(3.1) // 3
Math.ceil(3.1) // 4
Math.floor(3.1) // 3
// bitwise round
// all bitwise operators remove fractional part of number,
// so any operation that will not change the number, round it.
~~3.1 // 3
0.1 + 3 ^ 0 // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment