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

Commit c91c95c

Browse files
watildecaridy
authored andcommitted
NumberFormat: update toFixed and use decimalAdjust
1 parent 4c219d3 commit c91c95c

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

src/11.numberformat.js

+46-40
Original file line numberDiff line numberDiff line change
@@ -794,62 +794,68 @@ function ToRawPrecision (x, minPrecision, maxPrecision) {
794794
}
795795

796796
/**
797+
* @spec[tc39/ecma402/master/spec/numberformat.html]
798+
* @clause[sec-torawfixed]
797799
* When the ToRawFixed abstract operation is called with arguments x (which must
798800
* be a finite non-negative number), minInteger (which must be an integer between
799801
* 1 and 21), minFraction, and maxFraction (which must be integers between 0 and
800802
* 20) the following steps are taken:
801803
*/
802-
function ToRawFixed (x, minInteger, minFraction, maxFraction) {
803-
// (or not because Number.toPrototype.toFixed does a lot of it for us)
804-
let idx,
805-
806-
// We can pick up after the fixed formatted string (m) is created
807-
m = Number.prototype.toFixed.call(x, maxFraction),
808-
809-
// 4. If [maxFraction] ≠ 0, then
810-
// ...
811-
// e. Let int be the number of characters in a.
812-
//
813-
// 5. Else let int be the number of characters in m.
814-
igr = m.split(".")[0].length, // int is a reserved word
815-
816-
// 6. Let cut be maxFraction – minFraction.
817-
cut = maxFraction - minFraction,
818-
819-
exp = (idx = m.indexOf('e')) > -1 ? m.slice(idx + 1) : 0;
820-
821-
if (exp) {
822-
m = m.slice(0, idx).replace('.', '');
823-
m += arrJoin.call(Array(exp - (m.length - 1) + 1), '0')
824-
+ '.' + arrJoin.call(Array(maxFraction + 1), '0');
825-
826-
igr = m.length;
804+
function ToRawFixed(x, minInteger, minFraction, maxFraction) {
805+
// 1. Let f be maxFraction.
806+
let f = maxFraction;
807+
// 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.
808+
let p = Math.pow(10, f) * x; // ###TODO: add description for this variable p
809+
let n = p > Math.floor(p) ? Math.round(p) : p + 1;
810+
// 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).
811+
let m = (n === 0 ? "0" : (n + '').split('.')[0]);
812+
813+
let int;
814+
// 4. If f ≠ 0, then
815+
if (f !== 0) {
816+
// a. Let k be the number of characters in m.
817+
let k = m.length;
818+
// a. If k ≤ f, then
819+
if (k <= f) {
820+
// i. Let z be the String consisting of f+1–k occurrences of the character "0".
821+
let z = arrJoin.call(Array(f + 1 - k + 1), '0');
822+
// ii. Let m be the concatenation of Strings z and m.
823+
m = z + m;
824+
// iii. Let k be f+1.
825+
k = f + 1;
826+
}
827+
// a. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
828+
let a = m.slice(0, k - f), b = m.slice(-(k - f));
829+
// a. Let m be the concatenation of the three Strings a, ".", and b.
830+
m = a + "." + b;
831+
// a. Let int be the number of characters in a.
832+
int = a.length;
827833
}
828-
834+
// 5. Else, let int be the number of characters in m.
835+
else int = m.length;
836+
// 6. Let cut be maxFraction – minFraction.
837+
let cut = maxFraction - minFraction;
829838
// 7. Repeat while cut > 0 and the last character of m is "0":
830839
while (cut > 0 && m.slice(-1) === "0") {
831840
// a. Remove the last character from m.
832841
m = m.slice(0, -1);
833-
834-
// b. Decrease cut by 1.
842+
// a. Decrease cut by 1.
835843
cut--;
836844
}
837-
838845
// 8. If the last character of m is ".", then
839-
if (m.slice(-1) === ".")
846+
if (m.slice(-1) === ".") {
840847
// a. Remove the last character from m.
841848
m = m.slice(0, -1);
842-
843-
let z;
849+
}
844850
// 9. If int < minInteger, then
845-
if (igr < minInteger)
846-
// a. Let z be the String consisting of minInteger–int occurrences of the
847-
// character "0".
848-
z = arrJoin.call(Array(minInteger - igr + 1), '0');
849-
850-
// 10. Let m be the concatenation of Strings z and m.
851-
// 11. Return m.
852-
return (z ? z : '') + m;
851+
if (int < minInteger) {
852+
// a. Let z be the String consisting of minInteger–int occurrences of the character "0".
853+
let z = arrJoin.call(Array(minInteger - int + 1), '0');
854+
// a. Let m be the concatenation of Strings z and m.
855+
m = z + m;
856+
}
857+
// 10. Return m.
858+
return m;
853859
}
854860

855861
// Sect 11.3.2 Table 2, Numbering systems

0 commit comments

Comments
 (0)