toString.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. runTest('new Number("444" + "123")');
  6. runTest('new Number(-444123)');
  7. runTest('new Number("444" + "123.789123456789875436")');
  8. runTest('new Number(-444123.78963636363636363636)');
  9. runTest('new Number(0)');
  10. runTest('0.9999999999999999e21');
  11. runTest('1e21');
  12. runTest('1.0000000000000001e21');
  13. runTest('-1.0000000000000001e21');
  14. function runTest(numberToTestAsString)
  15. {
  16. writeLine("Test: " + numberToTestAsString);
  17. var n = eval(numberToTestAsString + ";");
  18. writeLine("n.toString(): " + n.toString());
  19. writeLine("n.toString(10): " + n.toString(10));
  20. writeLine("n.toString(8): " + n.toString(8));
  21. writeLine("n.toString(2): " + n.toString(2));
  22. writeLine("n.toString(16): " + n.toString(16));
  23. writeLine("n.toString(25): " + n.toString(25));
  24. writeLine("n.toFixed(): " + n.toFixed());
  25. writeLine("n.toFixed(0): " + n.toFixed(0));
  26. writeLine("n.toFixed(2): " + n.toFixed(2));
  27. writeLine("n.toFixed(5): " + n.toFixed(5));
  28. writeLine("n.toFixed(20): " + n.toFixed(20));
  29. safeCall(function () { n.toFixed(-1); });
  30. safeCall(function () { n.toFixed(21); });
  31. writeLine("n.toExponential(): " + n.toExponential());
  32. writeLine("n.toExponential(undefined): " + n.toExponential(undefined));
  33. writeLine("n.toExponential(2): " + n.toExponential(2));
  34. writeLine("n.toExponential(5): " + n.toExponential(5));
  35. writeLine("n.toPrecision(): " + n.toPrecision());
  36. writeLine("n.toPrecision(2): " + n.toPrecision(2));
  37. writeLine("n.toPrecision(5): " + n.toPrecision(5));
  38. writeLine("n.toPrecision(20): " + n.toPrecision(20));
  39. // test toFixed toString round formatting
  40. if ( !(1.25499999999999989342.toFixed(2) + "" == "1.25") ||
  41. !(1.255.toFixed(2) + "" == "1.25") ||
  42. !(1.245.toFixed(2) + "" == "1.25") ||
  43. !(8.255.toFixed(2) + "" == "8.26") ) {
  44. throw Error("1.255.toFixed(2) != 1.25 or 8.255.toFixed(2) != 8.26 ??");
  45. }
  46. var rstr = "";
  47. for(var i = 0.0001; i < 0.001; i += 0.0001) {
  48. rstr += i.toPrecision(3) + "";
  49. }
  50. if (rstr != "0.0001000.0002000.0003000.0004000.0005000.0006000.0007000.0008000.000900") {
  51. throw new Error(rstr + " != " +
  52. "0.0001000.0002000.0003000.0004000.0005000.0006000.0007000.0008000.000900");
  53. }
  54. if (-4.223372036854776e+12 + "" != -4.223372036854776e+12.toFixed(3)) {
  55. // original number is;
  56. // -4223372036854.77587890625
  57. // We don't know the 8 after 775
  58. // Our default approach is to pick upperBound
  59. throw Error("-4.223372036854776e+12 -> -4223372036854.776");
  60. }
  61. writeLine("");
  62. }
  63. // Helpers
  64. function writeLine(str)
  65. {
  66. WScript.Echo("" + str);
  67. }
  68. function safeCall(func)
  69. {
  70. try
  71. {
  72. return func();
  73. }
  74. catch (ex)
  75. {
  76. writeLine(ex.name + ": " + ex.message);
  77. }
  78. }