MCPcopy Create free account
hub / github.com/angular-ui/ui-grid / roundNumber

Function roundNumber

lib/test/angular/1.6.7/angular.js:21445–21500  ·  view source on GitHub ↗

* Round the parsed number to the specified number of decimal places * This function changed the parsedNumber in-place

(parsedNumber, fractionSize, minFrac, maxFrac)

Source from the content-addressed store, hash-verified

21443 * This function changed the parsedNumber in-place
21444 */
21445function roundNumber(parsedNumber, fractionSize, minFrac, maxFrac) {
21446 var digits = parsedNumber.d;
21447 var fractionLen = digits.length - parsedNumber.i;
21448
21449 // determine fractionSize if it is not specified; `+fractionSize` converts it to a number
21450 fractionSize = (isUndefined(fractionSize)) ? Math.min(Math.max(minFrac, fractionLen), maxFrac) : +fractionSize;
21451
21452 // The index of the digit to where rounding is to occur
21453 var roundAt = fractionSize + parsedNumber.i;
21454 var digit = digits[roundAt];
21455
21456 if (roundAt > 0) {
21457 // Drop fractional digits beyond `roundAt`
21458 digits.splice(Math.max(parsedNumber.i, roundAt));
21459
21460 // Set non-fractional digits beyond `roundAt` to 0
21461 for (var j = roundAt; j < digits.length; j++) {
21462 digits[j] = 0;
21463 }
21464 } else {
21465 // We rounded to zero so reset the parsedNumber
21466 fractionLen = Math.max(0, fractionLen);
21467 parsedNumber.i = 1;
21468 digits.length = Math.max(1, roundAt = fractionSize + 1);
21469 digits[0] = 0;
21470 for (var i = 1; i < roundAt; i++) digits[i] = 0;
21471 }
21472
21473 if (digit >= 5) {
21474 if (roundAt - 1 < 0) {
21475 for (var k = 0; k > roundAt; k--) {
21476 digits.unshift(0);
21477 parsedNumber.i++;
21478 }
21479 digits.unshift(1);
21480 parsedNumber.i++;
21481 } else {
21482 digits[roundAt - 1]++;
21483 }
21484 }
21485
21486 // Pad out with zeros to get the required fraction length
21487 for (; fractionLen < Math.max(0, fractionSize); fractionLen++) digits.push(0);
21488
21489
21490 // Do any carrying, e.g. a digit was rounded up to 10
21491 var carry = digits.reduceRight(function(carry, d, i, digits) {
21492 d = d + carry;
21493 digits[i] = d % 10;
21494 return Math.floor(d / 10);
21495 }, 0);
21496 if (carry) {
21497 digits.unshift(carry);
21498 parsedNumber.i++;
21499 }
21500}
21501
21502/**

Callers 1

formatNumberFunction · 0.70

Calls 1

isUndefinedFunction · 0.70

Tested by

no test coverage detected