frontend/.pnpm-store/v3/files/de/99c84ebe91bc20388419d829c772f76ba79cba26864ea87c5cd0f62d27280afad0e254a1803096213d62fc17a591ac75c5cc5d737d5e0f740fc319660ff066-exec

56 lines
1.7 KiB
Plaintext
Executable File

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToRawFixed = void 0;
var utils_1 = require("../utils");
/**
* TODO: dedup with intl-pluralrules and support BigInt
* https://tc39.es/ecma402/#sec-torawfixed
* @param x a finite non-negative Number or BigInt
* @param minFraction and integer between 0 and 20
* @param maxFraction and integer between 0 and 20
*/
function ToRawFixed(x, minFraction, maxFraction) {
var f = maxFraction;
var n = Math.round(x * Math.pow(10, f));
var xFinal = n / Math.pow(10, f);
// n is a positive integer, but it is possible to be greater than 1e21.
// In such case we will go the slow path.
// See also: https://tc39.es/ecma262/#sec-numeric-types-number-tostring
var m;
if (n < 1e21) {
m = n.toString();
}
else {
m = n.toString();
var _a = m.split('e'), mantissa = _a[0], exponent = _a[1];
m = mantissa.replace('.', '');
m = m + (0, utils_1.repeat)('0', Math.max(+exponent - m.length + 1, 0));
}
var int;
if (f !== 0) {
var k = m.length;
if (k <= f) {
var z = (0, utils_1.repeat)('0', f + 1 - k);
m = z + m;
k = f + 1;
}
var a = m.slice(0, k - f);
var b = m.slice(k - f);
m = "".concat(a, ".").concat(b);
int = a.length;
}
else {
int = m.length;
}
var cut = maxFraction - minFraction;
while (cut > 0 && m[m.length - 1] === '0') {
m = m.slice(0, -1);
cut--;
}
if (m[m.length - 1] === '.') {
m = m.slice(0, -1);
}
return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
}
exports.ToRawFixed = ToRawFixed;