52 lines
1.6 KiB
Plaintext
Executable File
52 lines
1.6 KiB
Plaintext
Executable File
import { repeat } from '../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
|
|
*/
|
|
export 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 + repeat('0', Math.max(+exponent - m.length + 1, 0));
|
|
}
|
|
var int;
|
|
if (f !== 0) {
|
|
var k = m.length;
|
|
if (k <= f) {
|
|
var z = 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 };
|
|
}
|