JavaScript Numbers

01.23.2015

toFixed() will select exact amount of decimals to display.

parseFloat() turns strings with decimals into numbers.

Use both in combination to get exact values in math operations.

parseInt() takes first available integer at the front of string.

Use a radix value to ensure correct parsing. parseInt("021", 10);. This will ensure base 10 parsing.

To test for a number, use typeof AND isNAN()

function isThisActuallyANumberDontLie(data){
  return (typeof data === "number" && !isNaN(data) );
}

Sources

Code School JavaScript Best Practices