1 // Copyright (C) 2016 The V8 Project authors. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
5 esid: sec-number.prototype.toprecision
7 Return regular string values
9 Number.prototype.toPrecision ( precision )
11 1. Let x be ? thisNumberValue(this value).
13 5. Let s be the empty String.
15 11. If e = p-1, return the concatenation of the Strings s and m.
17 a. Let m be the concatenation of the first e+1 elements of m, the code unit
18 0x002E (FULL STOP), and the remaining p- (e+1) elements of m.
20 a. Let m be the String formed by the concatenation of code unit 0x0030
21 (DIGIT ZERO), code unit 0x002E (FULL STOP), -(e+1) occurrences of code unit
22 0x0030 (DIGIT ZERO), and the String m.
23 14. Return the String that is the concatenation of s and m.
26 assert.sameValue((7).toPrecision(1), "7");
27 assert.sameValue((7).toPrecision(2), "7.0");
28 assert.sameValue((7).toPrecision(3), "7.00");
29 assert.sameValue((7).toPrecision(19), "7.000000000000000000");
30 assert.sameValue((7).toPrecision(20), "7.0000000000000000000");
31 assert.sameValue((7).toPrecision(21), "7.00000000000000000000");
33 assert.sameValue((-7).toPrecision(1), "-7");
34 assert.sameValue((-7).toPrecision(2), "-7.0");
35 assert.sameValue((-7).toPrecision(3), "-7.00");
36 assert.sameValue((-7).toPrecision(19), "-7.000000000000000000");
37 assert.sameValue((-7).toPrecision(20), "-7.0000000000000000000");
38 assert.sameValue((-7).toPrecision(21), "-7.00000000000000000000");
40 assert.sameValue((10).toPrecision(2), "10");
41 assert.sameValue((11).toPrecision(2), "11");
42 assert.sameValue((17).toPrecision(2), "17");
43 assert.sameValue((19).toPrecision(2), "19");
44 assert.sameValue((20).toPrecision(2), "20");
46 assert.sameValue((-10).toPrecision(2), "-10");
47 assert.sameValue((-11).toPrecision(2), "-11");
48 assert.sameValue((-17).toPrecision(2), "-17");
49 assert.sameValue((-19).toPrecision(2), "-19");
50 assert.sameValue((-20).toPrecision(2), "-20");
52 assert.sameValue((42).toPrecision(2), "42");
53 assert.sameValue((-42).toPrecision(2), "-42");
55 assert.sameValue((100).toPrecision(3), "100");
56 assert.sameValue((100).toPrecision(7), "100.0000");
57 assert.sameValue((1000).toPrecision(7), "1000.000");
58 assert.sameValue((10000).toPrecision(7), "10000.00");
59 assert.sameValue((100000).toPrecision(7), "100000.0");
61 assert.sameValue((0.000001).toPrecision(1), "0.000001");
62 assert.sameValue((0.000001).toPrecision(2), "0.0000010");
63 assert.sameValue((0.000001).toPrecision(3), "0.00000100");