artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
_createRound.js
1 var toInteger = require('./toInteger'),
2  toNumber = require('./toNumber'),
3  toString = require('./toString');
4 
5 /* Built-in method references for those with the same name as other `lodash` methods. */
6 var nativeMin = Math.min;
7 
15 function createRound(methodName) {
16  var func = Math[methodName];
17  return function(number, precision) {
18  number = toNumber(number);
19  precision = precision == null ? 0 : nativeMin(toInteger(precision), 292);
20  if (precision) {
21  // Shift with exponential notation to avoid floating-point issues.
22  // See [MDN](https://mdn.io/round#Examples) for more details.
23  var pair = (toString(number) + 'e').split('e'),
24  value = func(pair[0] + 'e' + (+pair[1] + precision));
25 
26  pair = (toString(value) + 'e').split('e');
27  return +(pair[0] + 'e' + (+pair[1] - precision));
28  }
29  return func(number);
30  };
31 }
32 
33 module.exports = createRound;