number-tostring-func.js 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// ----------------------------------------------------------------------
// toString
function testToString(a, b) {
  assertEquals(a, b.toString());
}

function testToStringP(a, b, c) {
  assertEquals(a, b.toString(c));
}

testToString("NaN", (NaN));
testToString("Infinity", (1/0));
testToString("-Infinity", (-1/0));
testToString("0", (0));
testToString("9", (9));
testToString("90", (90));
testToString("90.12", (90.12));
testToString("0.1", (0.1));
testToString("0.01", (0.01));
testToString("0.0123", (0.0123));
testToString("111111111111111110000", (111111111111111111111));
testToString("1.1111111111111111e+21", (1111111111111111111111));
testToString("1.1111111111111111e+22", (11111111111111111111111));
testToString("0.00001", (0.00001));
testToString("0.000001", (0.000001));
testToString("1e-7", (0.0000001));
testToString("1.2e-7", (0.00000012));
testToString("1.23e-7", (0.000000123));
testToString("1e-8", (0.00000001));
testToString("1.2e-8", (0.000000012));
testToString("1.23e-8", (0.0000000123));

testToString("0", (-0));
testToString("-9", (-9));
testToString("-90", (-90));
testToString("-90.12", (-90.12));
testToString("-0.1", (-0.1));
testToString("-0.01", (-0.01));
testToString("-0.0123", (-0.0123));
testToString("-111111111111111110000", (-111111111111111111111));
testToString("-1.1111111111111111e+21", (-1111111111111111111111));
testToString("-1.1111111111111111e+22", (-11111111111111111111111));
testToString("-0.00001", (-0.00001));
testToString("-0.000001", (-0.000001));
testToString("-1e-7", (-0.0000001));
testToString("-1.2e-7", (-0.00000012));
testToString("-1.23e-7", (-0.000000123));
testToString("-1e-8", (-0.00000001));
testToString("-1.2e-8", (-0.000000012));
testToString("-1.23e-8", (-0.0000000123));

testToString("1000", (1000));
testToString("0.00001", (0.00001));
testToString("1000000000000000100", (1000000000000000128));
testToString("1e+21", (1000000000000000012800));
testToString("-1e+21", (-1000000000000000012800));
testToString("1e-7", (0.0000001));
testToString("-1e-7", (-0.0000001));
testToString("1.0000000000000001e+21", (1000000000000000128000));
testToString("0.000001", (0.000001));
testToString("1e-7", (0.0000001));

testToStringP("NaN", (NaN), 16);
testToStringP("Infinity", (1/0), 16);
testToStringP("-Infinity", (-1/0), 16);
testToStringP("0", (0), 16);
testToStringP("9", (9), 16);
testToStringP("5a", (90), 16);
testToStringP("5a.1eb851eb852", (90.12), 16);
testToStringP("0.1999999999999a", (0.1), 16);
testToStringP("0.028f5c28f5c28f6", (0.01), 16);
testToStringP("0.032617c1bda511a", (0.0123), 16);
testToStringP("605f9f6dd18bc8000", (111111111111111111111), 16);
testToStringP("3c3bc3a4a2f75c0000", (1111111111111111111111), 16);
testToStringP("25a55a46e5da9a00000", (11111111111111111111111), 16);
testToStringP("0.0000a7c5ac471b4788", (0.00001), 16);
testToStringP("0.000010c6f7a0b5ed8d", (0.000001), 16);
testToStringP("0.000001ad7f29abcaf48", (0.0000001), 16);
testToStringP("0.000002036565348d256", (0.00000012), 16);
testToStringP("0.0000021047ee22aa466", (0.000000123), 16);
testToStringP("0.0000002af31dc4611874", (0.00000001), 16);
testToStringP("0.000000338a23b87483be", (0.000000012), 16);
testToStringP("0.00000034d3fe36aaa0a2", (0.0000000123), 16);

testToStringP("0", (-0), 16);
testToStringP("-9", (-9), 16);
testToStringP("-5a", (-90), 16);
testToStringP("-5a.1eb851eb852", (-90.12), 16);
testToStringP("-0.1999999999999a", (-0.1), 16);
testToStringP("-0.028f5c28f5c28f6", (-0.01), 16);
testToStringP("-0.032617c1bda511a", (-0.0123), 16);
testToStringP("-605f9f6dd18bc8000", (-111111111111111111111), 16);
testToStringP("-3c3bc3a4a2f75c0000", (-1111111111111111111111), 16);
testToStringP("-25a55a46e5da9a00000", (-11111111111111111111111), 16);
testToStringP("-0.0000a7c5ac471b4788", (-0.00001), 16);
testToStringP("-0.000010c6f7a0b5ed8d", (-0.000001), 16);
testToStringP("-0.000001ad7f29abcaf48", (-0.0000001), 16);
testToStringP("-0.000002036565348d256", (-0.00000012), 16);
testToStringP("-0.0000021047ee22aa466", (-0.000000123), 16);
testToStringP("-0.0000002af31dc4611874", (-0.00000001), 16);
testToStringP("-0.000000338a23b87483be", (-0.000000012), 16);
testToStringP("-0.00000034d3fe36aaa0a2", (-0.0000000123), 16);

testToString("4294967296", Math.pow(2,32));
testToStringP("ffffffff", (Math.pow(2,32)-1), 16);
testToStringP("11111111111111111111111111111111", (Math.pow(2,32)-1), 2);
testToStringP("5yc1z", (10000007), 36);
testToStringP("0", (0), 36);
testToStringP("0", (0), 16);
testToStringP("0", (0), 10);
testToStringP("0", (0), 8);
testToStringP("0", (0), 2);
testToStringP("100000000000000000000000000000000", Math.pow(2,32), 2);
testToStringP("100000000000000000000000000000001", (Math.pow(2,32) + 1), 2);
testToStringP("100000000000080", (0x100000000000081), 16);
testToStringP("1000000000000100", (-(-'0x1000000000000081')), 16);
testToStringP("1000000000000000", (-(-'0x1000000000000080')), 16);
testToStringP("1000000000000000", (-(-'0x100000000000007F')), 16);
testToStringP("100000000000000000000000000000000000000000000000010000000", (0x100000000000081), 2);
testToStringP("-11111111111111111111111111111111", (-(Math.pow(2,32)-1)), 2);
testToStringP("-5yc1z", (-10000007), 36);
testToStringP("-100000000000000000000000000000000", (-Math.pow(2,32)), 2);
testToStringP("-100000000000000000000000000000001", (-(Math.pow(2,32) + 1)), 2);
testToStringP("-100000000000080", (-0x100000000000081), 16);
testToStringP("-100000000000000000000000000000000000000000000000010000000", (-0x100000000000081), 2);
testToStringP("8.8", (8.5), 16);
testToStringP("-8.8", (-8.5), 16);

// ----------------------------------------------------------------------
// toFixed
function testToFixed(a, b, c) {
  assertEquals(a, b.toFixed(c));
}

testToFixed("NaN", (NaN), (2));
testToFixed("Infinity", (1/0), (2));
testToFixed("-Infinity", (-1/0), (2));

testToFixed("1.1111111111111111e+21", (1111111111111111111111), (8));
testToFixed("0.1", (0.1), (1));
testToFixed("0.10", (0.1), (2));
testToFixed("0.100", (0.1), (3));
testToFixed("0.01", (0.01), (2));
testToFixed("0.010", (0.01), (3));
testToFixed("0.0100", (0.01), (4));
testToFixed("0.00", (0.001), (2));
testToFixed("0.001", (0.001), (3));
testToFixed("0.0010", (0.001), (4));
testToFixed("1.0000", (1), (4));
testToFixed("1.0", (1), (1));
testToFixed("1", (1), (0));
testToFixed("12", (12), (0));
testToFixed("1", (1.1), (0));
testToFixed("12", (12.1), (0));
testToFixed("1", (1.12), (0));
testToFixed("12", (12.12), (0));
testToFixed("0.0000006", (0.0000006), (7));
testToFixed("0.00000006", (0.00000006), (8));
testToFixed("0.000000060", (0.00000006), (9));
testToFixed("0.0000000600", (0.00000006), (10));
testToFixed("0", (0), (0));
testToFixed("0.0", (0), (1));
testToFixed("0.00", (0), (2));

testToFixed("-1.1111111111111111e+21", (-1111111111111111111111), (8));
testToFixed("-0.1", (-0.1), (1));
testToFixed("-0.10", (-0.1), (2));
testToFixed("-0.100", (-0.1), (3));
testToFixed("-0.01", (-0.01), (2));
testToFixed("-0.010", (-0.01), (3));
testToFixed("-0.0100", (-0.01), (4));
testToFixed("-0.00", (-0.001), (2));
testToFixed("-0.001", (-0.001), (3));
testToFixed("-0.0010", (-0.001), (4));
testToFixed("-1.0000", (-1), (4));
testToFixed("-1.0", (-1), (1));
testToFixed("-1", (-1), (0));
testToFixed("-1", (-1.1), (0));
testToFixed("-12", (-12.1), (0));
testToFixed("-1", (-1.12), (0));
testToFixed("-12", (-12.12), (0));
testToFixed("-0.0000006", (-0.0000006), (7));
testToFixed("-0.00000006", (-0.00000006), (8));
testToFixed("-0.000000060", (-0.00000006), (9));
testToFixed("-0.0000000600", (-0.00000006), (10));
testToFixed("0", (-0), (0));
testToFixed("0.0", (-0), (1));
testToFixed("0.00", (-0), (2));

testToFixed("0.00001", (0.00001), (5));
testToFixed("0.00000000000000000010", (0.0000000000000000001), (20));
testToFixed("0.00001000000000000", (0.00001), (17));
testToFixed("1.00000000000000000", (1), (17));
testToFixed("100000000000000128.0", (100000000000000128), (1));
testToFixed("10000000000000128.00", (10000000000000128), (2));
testToFixed("10000000000000128.00000000000000000000", (10000000000000128), (20));
testToFixed("-42.000", (-42), (3));
testToFixed("-0.00000000000000000010", (-0.0000000000000000001), (20));
testToFixed("0.12312312312312299889", (0.123123123123123), (20));

assertEquals("-1000000000000000128", (-1000000000000000128).toFixed());
assertEquals("0", (0).toFixed());
assertEquals("1000000000000000128", (1000000000000000128).toFixed());
assertEquals("1000", (1000).toFixed());
assertEquals("0", (0.00001).toFixed());
// Test that we round up even when the last digit generated is even.
// dtoa does not do this in its original form.
assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)");
assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)");
assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)");
// This is bizare, but Spidermonkey and KJS behave the same.
assertEquals("234.2040", (234.20405).toFixed(4), "234.2040.toFixed(4)");
assertEquals("234.2041", (234.2040506).toFixed(4));

// ----------------------------------------------------------------------
// toExponential
function testToExponential(a, b) {
  assertEquals(a, b.toExponential());
}

function testToExponentialP(a, b, c) {
  assertEquals(a, b.toExponential(c));
}

testToExponential("1e+0", (1));
testToExponential("1.1e+1", (11));
testToExponential("1.12e+2", (112));
testToExponential("1e-1", (0.1));
testToExponential("1.1e-1", (0.11));
testToExponential("1.12e-1", (0.112));
testToExponential("-1e+0", (-1));
testToExponential("-1.1e+1", (-11));
testToExponential("-1.12e+2", (-112));
testToExponential("-1e-1", (-0.1));
testToExponential("-1.1e-1", (-0.11));
testToExponential("-1.12e-1", (-0.112));
testToExponential("0e+0", (0));
testToExponential("1.12356e-4", (0.000112356));
testToExponential("-1.12356e-4", (-0.000112356));

testToExponentialP("1e+0", (1), (0));
testToExponentialP("1e+1", (11), (0));
testToExponentialP("1e+2", (112), (0));
testToExponentialP("1.0e+0", (1), (1));
testToExponentialP("1.1e+1", (11), (1));
testToExponentialP("1.1e+2", (112), (1));
testToExponentialP("1.00e+0", (1), (2));
testToExponentialP("1.10e+1", (11), (2));
testToExponentialP("1.12e+2", (112), (2));
testToExponentialP("1.000e+0", (1), (3));
testToExponentialP("1.100e+1", (11), (3));
testToExponentialP("1.120e+2", (112), (3));
testToExponentialP("1e-1", (0.1), (0));
testToExponentialP("1e-1", (0.11), (0));
testToExponentialP("1e-1", (0.112), (0));
testToExponentialP("1.0e-1", (0.1), (1));
testToExponentialP("1.1e-1", (0.11), (1));
testToExponentialP("1.1e-1", (0.112), (1));
testToExponentialP("1.00e-1", (0.1), (2));
testToExponentialP("1.10e-1", (0.11), (2));
testToExponentialP("1.12e-1", (0.112), (2));
testToExponentialP("1.000e-1", (0.1), (3));
testToExponentialP("1.100e-1", (0.11), (3));
testToExponentialP("1.120e-1", (0.112), (3));

testToExponentialP("-1e+0", (-1), (0));
testToExponentialP("-1e+1", (-11), (0));
testToExponentialP("-1e+2", (-112), (0));
testToExponentialP("-1.0e+0", (-1), (1));
testToExponentialP("-1.1e+1", (-11), (1));
testToExponentialP("-1.1e+2", (-112), (1));
testToExponentialP("-1.00e+0", (-1), (2));
testToExponentialP("-1.10e+1", (-11), (2));
testToExponentialP("-1.12e+2", (-112), (2));
testToExponentialP("-1.000e+0", (-1), (3));
testToExponentialP("-1.100e+1", (-11), (3));
testToExponentialP("-1.120e+2", (-112), (3));
testToExponentialP("-1e-1", (-0.1), (0));
testToExponentialP("-1e-1", (-0.11), (0));
testToExponentialP("-1e-1", (-0.112), (0));
testToExponentialP("-1.0e-1", (-0.1), (1));
testToExponentialP("-1.1e-1", (-0.11), (1));
testToExponentialP("-1.1e-1", (-0.112), (1));
testToExponentialP("-1.00e-1", (-0.1), (2));
testToExponentialP("-1.10e-1", (-0.11), (2));
testToExponentialP("-1.12e-1", (-0.112), (2));
testToExponentialP("-1.000e-1", (-0.1), (3));
testToExponentialP("-1.100e-1", (-0.11), (3));
testToExponentialP("-1.120e-1", (-0.112), (3));

testToExponentialP("NaN", (NaN), (2));
testToExponentialP("Infinity", (Infinity), (2));
testToExponentialP("-Infinity", (-Infinity), (2));
testToExponentialP("1e+0", (1), (0));
testToExponentialP("0.00e+0", (0), (2));
testToExponentialP("1e+1", (11.2356), (0));
testToExponentialP("1.1236e+1", (11.2356), (4));
testToExponentialP("1.1236e-4", (0.000112356), (4));
testToExponentialP("-1.1236e-4", (-0.000112356), (4));

// ----------------------------------------------------------------------
// toPrecision
function testToPrecision(a, b, c) {
  assertEquals(a, b.toPrecision(c));
}

testToPrecision("NaN", (NaN), (1));
testToPrecision("Infinity", (Infinity), (2));
testToPrecision("-Infinity", (-Infinity), (2));
testToPrecision("0.000555000000000000", (0.000555), (15));
testToPrecision("5.55000000000000e-7", (0.000000555), (15));
testToPrecision("-5.55000000000000e-7", (-0.000000555), (15));
testToPrecision("1e+8", (123456789), (1));
testToPrecision("123456789", (123456789), (9));
testToPrecision("1.2345679e+8", (123456789), (8));
testToPrecision("1.234568e+8", (123456789), (7));
testToPrecision("-1.234568e+8", (-123456789), (7));
testToPrecision("-1.2e-9", Number(-.0000000012345), (2));
testToPrecision("-1.2e-8", Number(-.000000012345), (2));
testToPrecision("-1.2e-7", Number(-.00000012345), (2));
testToPrecision("-0.0000012", Number(-.0000012345), (2));
testToPrecision("-0.000012", Number(-.000012345), (2));
testToPrecision("-0.00012", Number(-.00012345), (2));
testToPrecision("-0.0012", Number(-.0012345), (2));
testToPrecision("-0.012", Number(-.012345), (2));
testToPrecision("-0.12", Number(-.12345), (2));
testToPrecision("-1.2", Number(-1.2345), (2));
testToPrecision("-12", Number(-12.345), (2));
testToPrecision("-1.2e+2", Number(-123.45), (2));
testToPrecision("-1.2e+3", Number(-1234.5), (2));
testToPrecision("-1.2e+4", Number(-12345), (2));
testToPrecision("-1.235e+4", Number(-12345.67), (4));
testToPrecision("-1.234e+4", Number(-12344.67), (4));
// Test that we round up even when the last digit generated is even.
// dtoa does not do this in its original form.
assertEquals("1.3", 1.25.toPrecision(2), "1.25.toPrecision(2)");
assertEquals("1.4", 1.35.toPrecision(2), "1.35.toPrecision(2)");