-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathNumberToFixed.ts
More file actions
14 lines (14 loc) · 707 Bytes
/
NumberToFixed.ts
File metadata and controls
14 lines (14 loc) · 707 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// https://www.ecma-international.org/ecma-262/10.0/index.html#sec-number.prototype.tofixed
export function __TS__NumberToFixed(this: number, fractionDigits?: number): string {
if (Math.abs(this) >= 1e21 || this !== this) {
return this.toString();
}
const f = Math.floor(fractionDigits ?? 0);
// reduced to 99 as string.format only supports 2-digit numbers
if (f < 0 || f > 99) {
throw "toFixed() digits argument must be between 0 and 99";
}
// throws "invalid format (width or precision too long)" if strlen > 99
// if (f < 80) return fmt; else try {return fmt} catch(_) { throw "toFixed() digits argument..." }
return string.format(`%.${f}f`, this);
}