Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 226f0b0

Browse files
committed
fix for the big numbers when attempting to format them
1 parent f8776a2 commit 226f0b0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/11.numberformat.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,20 @@ function ToRawFixed(x, minInteger, minFraction, maxFraction) {
804804
// 1. Let f be maxFraction.
805805
let f = maxFraction;
806806
// 2. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as possible. If there are two such n, pick the larger n.
807-
let n = Math.round(Math.pow(10, f) * x);
807+
let n = Math.pow(10, f) * x; // diverging...
808808
// 3. If n = 0, let m be the String "0". Otherwise, let m be the String consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
809-
let m = (n === 0 ? "0" : (n + '').split('.')[0]);
809+
let m = (n === 0 ? "0" : n.toFixed(0)); // divering...
810+
811+
{
812+
// this diversion is needed to take into consideration big numbers, e.g.:
813+
// 1.2344501e+37 -> 12344501000000000000000000000000000000
814+
let idx;
815+
let exp = (idx = m.indexOf('e')) > -1 ? m.slice(idx + 1) : 0;
816+
if (exp) {
817+
m = m.slice(0, idx).replace('.', '');
818+
m += arrJoin.call(Array(exp - (m.length - 1) + 1), '0');
819+
}
820+
}
810821

811822
let int;
812823
// 4. If f ≠ 0, then

0 commit comments

Comments
 (0)