|
Last change
on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Prototype 1.1
|
-
Property mode
set to
100644
|
|
File size:
843 bytes
|
| Line | |
|---|
| 1 | export default function(x) {
|
|---|
| 2 | return Math.abs(x = Math.round(x)) >= 1e21
|
|---|
| 3 | ? x.toLocaleString("en").replace(/,/g, "")
|
|---|
| 4 | : x.toString(10);
|
|---|
| 5 | }
|
|---|
| 6 |
|
|---|
| 7 | // Computes the decimal coefficient and exponent of the specified number x with
|
|---|
| 8 | // significant digits p, where x is positive and p is in [1, 21] or undefined.
|
|---|
| 9 | // For example, formatDecimalParts(1.23) returns ["123", 0].
|
|---|
| 10 | export function formatDecimalParts(x, p) {
|
|---|
| 11 | if (!isFinite(x) || x === 0) return null; // NaN, ±Infinity, ±0
|
|---|
| 12 | var i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e"), coefficient = x.slice(0, i);
|
|---|
| 13 |
|
|---|
| 14 | // The string returned by toExponential either has the form \d\.\d+e[-+]\d+
|
|---|
| 15 | // (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
|
|---|
| 16 | return [
|
|---|
| 17 | coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
|
|---|
| 18 | +x.slice(i + 1)
|
|---|
| 19 | ];
|
|---|
| 20 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.