source: imaps-frontend/node_modules/core-js/modules/es.number.to-exponential.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.9 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var uncurryThis = require('../internals/function-uncurry-this');
4var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
5var thisNumberValue = require('../internals/this-number-value');
6var $repeat = require('../internals/string-repeat');
7var log10 = require('../internals/math-log10');
8var fails = require('../internals/fails');
9
10var $RangeError = RangeError;
11var $String = String;
12var $isFinite = isFinite;
13var abs = Math.abs;
14var floor = Math.floor;
15var pow = Math.pow;
16var round = Math.round;
17var nativeToExponential = uncurryThis(1.0.toExponential);
18var repeat = uncurryThis($repeat);
19var stringSlice = uncurryThis(''.slice);
20
21// Edge 17-
22var ROUNDS_PROPERLY = nativeToExponential(-6.9e-11, 4) === '-6.9000e-11'
23 // IE11- && Edge 14-
24 && nativeToExponential(1.255, 2) === '1.25e+0'
25 // FF86-, V8 ~ Chrome 49-50
26 && nativeToExponential(12345, 3) === '1.235e+4'
27 // FF86-, V8 ~ Chrome 49-50
28 && nativeToExponential(25, 0) === '3e+1';
29
30// IE8-
31var throwsOnInfinityFraction = function () {
32 return fails(function () {
33 nativeToExponential(1, Infinity);
34 }) && fails(function () {
35 nativeToExponential(1, -Infinity);
36 });
37};
38
39// Safari <11 && FF <50
40var properNonFiniteThisCheck = function () {
41 return !fails(function () {
42 nativeToExponential(Infinity, Infinity);
43 nativeToExponential(NaN, Infinity);
44 });
45};
46
47var FORCED = !ROUNDS_PROPERLY || !throwsOnInfinityFraction() || !properNonFiniteThisCheck();
48
49// `Number.prototype.toExponential` method
50// https://tc39.es/ecma262/#sec-number.prototype.toexponential
51$({ target: 'Number', proto: true, forced: FORCED }, {
52 toExponential: function toExponential(fractionDigits) {
53 var x = thisNumberValue(this);
54 if (fractionDigits === undefined) return nativeToExponential(x);
55 var f = toIntegerOrInfinity(fractionDigits);
56 if (!$isFinite(x)) return String(x);
57 // TODO: ES2018 increased the maximum number of fraction digits to 100, need to improve the implementation
58 if (f < 0 || f > 20) throw new $RangeError('Incorrect fraction digits');
59 if (ROUNDS_PROPERLY) return nativeToExponential(x, f);
60 var s = '';
61 var m, e, c, d;
62 if (x < 0) {
63 s = '-';
64 x = -x;
65 }
66 if (x === 0) {
67 e = 0;
68 m = repeat('0', f + 1);
69 } else {
70 // this block is based on https://gist.github.com/SheetJSDev/1100ad56b9f856c95299ed0e068eea08
71 // TODO: improve accuracy with big fraction digits
72 var l = log10(x);
73 e = floor(l);
74 var w = pow(10, e - f);
75 var n = round(x / w);
76 if (2 * x >= (2 * n + 1) * w) {
77 n += 1;
78 }
79 if (n >= pow(10, f + 1)) {
80 n /= 10;
81 e += 1;
82 }
83 m = $String(n);
84 }
85 if (f !== 0) {
86 m = stringSlice(m, 0, 1) + '.' + stringSlice(m, 1);
87 }
88 if (e === 0) {
89 c = '+';
90 d = '0';
91 } else {
92 c = e > 0 ? '+' : '-';
93 d = $String(abs(e));
94 }
95 m += 'e' + c + d;
96 return s + m;
97 }
98});
Note: See TracBrowser for help on using the repository browser.