toString.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function runTest(numberToTestAsString)
  14. {
  15. writeLine("Test: " + numberToTestAsString);
  16. var n = eval(numberToTestAsString + ";");
  17. writeLine("n.toString(): " + n.toString());
  18. writeLine("n.toString(10): " + n.toString(10));
  19. writeLine("n.toString(8): " + n.toString(8));
  20. writeLine("n.toString(2): " + n.toString(2));
  21. writeLine("n.toString(16): " + n.toString(16));
  22. writeLine("n.toString(25): " + n.toString(25));
  23. writeLine("n.toFixed(): " + n.toFixed());
  24. writeLine("n.toFixed(0): " + n.toFixed(0));
  25. writeLine("n.toFixed(2): " + n.toFixed(2));
  26. writeLine("n.toFixed(5): " + n.toFixed(5));
  27. writeLine("n.toFixed(20): " + n.toFixed(20));
  28. safeCall(function () { n.toFixed(-1); });
  29. safeCall(function () { n.toFixed(21); });
  30. writeLine("n.toExponential(): " + n.toExponential());
  31. writeLine("n.toExponential(undefined): " + n.toExponential(undefined));
  32. writeLine("n.toExponential(2): " + n.toExponential(2));
  33. writeLine("n.toExponential(5): " + n.toExponential(5));
  34. writeLine("n.toPrecision(): " + n.toPrecision());
  35. writeLine("n.toPrecision(2): " + n.toPrecision(2));
  36. writeLine("n.toPrecision(5): " + n.toPrecision(5));
  37. writeLine("n.toPrecision(20): " + n.toPrecision(20));
  38. // test toFixed toString round formatting
  39. if ( !(1.25499999999999989342.toFixed(2) + "" == "1.25") ||
  40. !(1.255.toFixed(2) + "" == "1.25") ||
  41. !(1.245.toFixed(2) + "" == "1.25") ||
  42. !(8.255.toFixed(2) + "" == "8.26") ) {
  43. throw Error("1.255.toFixed(2) != 1.25 or 8.255.toFixed(2) != 8.26 ??");
  44. }
  45. if (-4.223372036854776e+12 + "" != -4.223372036854776e+12.toFixed(3)) {
  46. // original number is;
  47. // -4223372036854.77587890625
  48. // We don't know the 8 after 775
  49. // Our default approach is to pick upperBound
  50. throw Error("-4.223372036854776e+12 -> -4223372036854.776");
  51. }
  52. writeLine("");
  53. }
  54. // Helpers
  55. function writeLine(str)
  56. {
  57. WScript.Echo("" + str);
  58. }
  59. function safeCall(func)
  60. {
  61. try
  62. {
  63. return func();
  64. }
  65. catch (ex)
  66. {
  67. writeLine(ex.name + ": " + ex.message);
  68. }
  69. }