* Round the parsed number to the specified number of decimal places * This function changed the parsedNumber in-place
(parsedNumber, fractionSize, minFrac, maxFrac)
| 20106 | * This function changed the parsedNumber in-place |
| 20107 | */ |
| 20108 | function roundNumber(parsedNumber, fractionSize, minFrac, maxFrac) { |
| 20109 | var digits = parsedNumber.d; |
| 20110 | var fractionLen = digits.length - parsedNumber.i; |
| 20111 | |
| 20112 | // determine fractionSize if it is not specified; `+fractionSize` converts it to a number |
| 20113 | fractionSize = (isUndefined(fractionSize)) ? Math.min(Math.max(minFrac, fractionLen), maxFrac) : +fractionSize; |
| 20114 | |
| 20115 | // The index of the digit to where rounding is to occur |
| 20116 | var roundAt = fractionSize + parsedNumber.i; |
| 20117 | var digit = digits[roundAt]; |
| 20118 | |
| 20119 | if (roundAt > 0) { |
| 20120 | digits.splice(roundAt); |
| 20121 | } else { |
| 20122 | // We rounded to zero so reset the parsedNumber |
| 20123 | parsedNumber.i = 1; |
| 20124 | digits.length = roundAt = fractionSize + 1; |
| 20125 | for (var i = 0; i < roundAt; i++) digits[i] = 0; |
| 20126 | } |
| 20127 | |
| 20128 | if (digit >= 5) digits[roundAt - 1]++; |
| 20129 | |
| 20130 | // Pad out with zeros to get the required fraction length |
| 20131 | for (; fractionLen < fractionSize; fractionLen++) digits.push(0); |
| 20132 | |
| 20133 | // Do any carrying, e.g. a digit was rounded up to 10 |
| 20134 | var carry = digits.reduceRight(function (carry, d, i, digits) { |
| 20135 | d = d + carry; |
| 20136 | digits[i] = d % 10; |
| 20137 | return Math.floor(d / 10); |
| 20138 | }, |
| 20139 | 0); |
| 20140 | if (carry) { |
| 20141 | digits.unshift(carry); |
| 20142 | parsedNumber.i++; |
| 20143 | } |
| 20144 | } |
| 20145 | |
| 20146 | /** |
| 20147 | * Format a number into a string |
no test coverage detected