Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 997 Bytes

number.md

File metadata and controls

53 lines (39 loc) · 997 Bytes

Number

exponent

Syntactic sugar of Math.pow.

Math.pow(7, 3); // 343
7 *** 3 // 343
4 ** 2 // 16

currency-formatting

const number = 5890;
number.toLocaleString('en', {style: 'currency', currency: 'GBP'} // £5,890.00

precisive-rounding

function round(number, precision) {
  var shift = function (number, precision, reverseShift) {
    if (reverseShift) {
      precision = -precision;
    }
    var numArray = ("" + number).split("e");
    return +(numArray[0] + "e" + (numArray[1] ? (+numArray[1] + precision) : precision));
  };
  return shift(Math.round(shift(number, precision, false)), precision, true);
}

use-unary-for-number-conversion

+3     // 3
+'3'   // 3
+true  // 1
+false // 0
+null  // 0
+function(val){ return val } // NaN