nerdamer-ts
Version:
javascript light-weight symbolic math expression evaluator
38 lines • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.round = void 0;
const abs_1 = require("./abs");
const Symbol_1 = require("../../../Types/Symbol");
const Utils_1 = require("../../../Core/Utils");
const index_1 = require("../index");
/**
* Round a number up to s decimal places
* @param {Symbol} x
* @param {int} s - the number of decimal places
* @returns {undefined}
*/
function round(x, s) {
var sIsConstant = s && s.isConstant() || typeof s === 'undefined';
if (x.isConstant() && sIsConstant) {
var v, e, exp, retval;
v = x;
//round the coefficient of then number but not the actual decimal value
//we know this because a negative number was passed
if (s && s.lessThan(0)) {
s = (0, abs_1.abs)(s);
//convert the number to exponential form
e = Number(x).toExponential().toString().split('e');
//point v to the coefficient of then number
v = e[0];
//set the expontent
exp = e[1];
}
//round the number to the requested precision
retval = new Symbol_1.Symbol((0, Utils_1.nround)(v, Number(s || 0)));
//if there's a exponent then put it back
return (0, index_1.multiply)(retval, (0, index_1.pow)(new Symbol_1.Symbol(10), new Symbol_1.Symbol(exp || 0)));
}
return (0, Symbol_1.symfunction)('round', arguments);
}
exports.round = round;
//# sourceMappingURL=round.js.map