toString.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (!numberToTestAsString.endsWith('e21')) {
  24. // Different results on Linux arm64 due to some rounding errors
  25. // TODO: check Js::NumberUtilities::FNonZeroFiniteDblToStr()
  26. writeLine("n.toString(25): " + n.toString(25));
  27. }
  28. writeLine("n.toFixed(): " + n.toFixed());
  29. writeLine("n.toFixed(0): " + n.toFixed(0));
  30. writeLine("n.toFixed(2): " + n.toFixed(2));
  31. writeLine("n.toFixed(5): " + n.toFixed(5));
  32. writeLine("n.toFixed(20): " + n.toFixed(20));
  33. safeCall(function () { n.toFixed(-1); });
  34. safeCall(function () { n.toFixed(21); });
  35. writeLine("n.toExponential(): " + n.toExponential());
  36. writeLine("n.toExponential(undefined): " + n.toExponential(undefined));
  37. writeLine("n.toExponential(2): " + n.toExponential(2));
  38. writeLine("n.toExponential(5): " + n.toExponential(5));
  39. writeLine("n.toPrecision(): " + n.toPrecision());
  40. writeLine("n.toPrecision(2): " + n.toPrecision(2));
  41. writeLine("n.toPrecision(5): " + n.toPrecision(5));
  42. writeLine("n.toPrecision(20): " + n.toPrecision(20));
  43. // test toFixed toString round formatting
  44. if ( !(1.25499999999999989342.toFixed(2) + "" == "1.25") ||
  45. !(1.255.toFixed(2) + "" == "1.25") ||
  46. !(1.245.toFixed(2) + "" == "1.25") ||
  47. !(8.255.toFixed(2) + "" == "8.26") ) {
  48. throw Error("1.255.toFixed(2) != 1.25 or 8.255.toFixed(2) != 8.26 ??");
  49. }
  50. var rstr = "";
  51. for(var i = 0.0001; i < 0.001; i += 0.0001) {
  52. rstr += i.toPrecision(3) + "";
  53. }
  54. if (rstr != "0.0001000.0002000.0003000.0004000.0005000.0006000.0007000.0008000.000900") {
  55. throw new Error(rstr + " != " +
  56. "0.0001000.0002000.0003000.0004000.0005000.0006000.0007000.0008000.000900");
  57. }
  58. if (-4.223372036854776e+12 + "" != -4.223372036854776e+12.toFixed(3)) {
  59. // original number is;
  60. // -4223372036854.77587890625
  61. // We don't know the 8 after 775
  62. // Our default approach is to pick upperBound
  63. throw Error("-4.223372036854776e+12 -> -4223372036854.776");
  64. }
  65. writeLine("");
  66. }
  67. // Helpers
  68. function writeLine(str)
  69. {
  70. WScript.Echo("" + str);
  71. }
  72. function safeCall(func)
  73. {
  74. try
  75. {
  76. return func();
  77. }
  78. catch (ex)
  79. {
  80. writeLine(ex.name + ": " + ex.message);
  81. }
  82. }