• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//This test will not pass on FLOAT32 due to precision issues
16
17assert((123.56).toPrecision() === "123.56");
18assert((123.56).toPrecision(1) === "1e+2");
19assert((123.56).toPrecision(2) === "1.2e+2");
20assert((123.56).toPrecision(6) === "123.560");
21assert((-1.23).toPrecision(1) === "-1");
22assert((0.00023).toPrecision(1) === "0.0002");
23assert((0.356).toPrecision(2) === "0.36");
24assert((0.0000356).toPrecision(5) === "0.000035600");
25assert((0.000030056).toPrecision(4) === "0.00003006");
26assert(Infinity.toPrecision(1) === "Infinity");
27assert((-Infinity).toPrecision(1) === "-Infinity");
28assert(NaN.toPrecision(1) === "NaN");
29assert((0.0).toPrecision(1) === "0");
30assert((-0.0).toPrecision(1) === "0");
31assert((0.0).toPrecision(6) === "0.00000");
32assert((123456789012345678901.0).toPrecision(20) === "1.2345678901234568000e+20");
33assert((123456789012345678901.0).toPrecision(21) === "123456789012345680000");
34assert((123456789012345678901.0).toPrecision("6") === "1.23457e+20");
35
36assert((123.56).toPrecision(1.3) === "1e+2");
37assert((123.56).toPrecision(21.9) === "123.560000000000000000");
38
39assert(Number(982).toPrecision(1) === "1e+3");
40assert(Number(982).toPrecision(2) === "9.8e+2");
41assert(Number(1499).toPrecision(1) === "1e+3");
42assert(Number(1500).toPrecision(1) === "2e+3");
43
44try {
45    (12).toPrecision(0);
46    assert(false);
47} catch (e) {
48    assert(e instanceof RangeError)
49}
50
51try {
52    (12).toPrecision(22);
53    assert(false);
54} catch (e) {
55    assert(e instanceof RangeError)
56}
57
58try {
59    Number.prototype.toExponential.call(new Object());
60    assert(false);
61} catch (e) {
62    assert(e instanceof TypeError)
63}
64