{"ScriptPreparationCode":"// Solution 1\r\nvar DecimalPrecision = (function() {\r\n if (Math.trunc === undefined) {\r\n Math.trunc = function(v) {\r\n return v \u003C 0 ? Math.ceil(v) : Math.floor(v);\r\n };\r\n }\r\n var decimalAdjust = function myself(type, num, decimalPlaces) {\r\n if (type === \u0027round\u0027 \u0026\u0026 num \u003C 0)\r\n return -myself(type, -num, decimalPlaces);\r\n var shift = function(value, exponent) {\r\n value = (value \u002B \u0027e\u0027).split(\u0027e\u0027);\r\n return \u002B(value[0] \u002B \u0027e\u0027 \u002B (\u002Bvalue[1] \u002B (exponent || 0)));\r\n };\r\n var n = shift(num, \u002BdecimalPlaces);\r\n return shift(Math[type](n), -decimalPlaces);\r\n };\r\n return {\r\n // Decimal round (half away from zero)\r\n round: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces);\r\n },\r\n // Decimal ceil\r\n ceil: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027ceil\u0027, num, decimalPlaces);\r\n },\r\n // Decimal floor\r\n floor: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027floor\u0027, num, decimalPlaces);\r\n },\r\n // Decimal trunc\r\n trunc: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027trunc\u0027, num, decimalPlaces);\r\n },\r\n // Format using fixed-point notation\r\n toFixed: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces).toFixed(decimalPlaces);\r\n }\r\n };\r\n})();\r\n\r\n// Solution 2\r\nvar DecimalPrecision2 = (function() {\r\n if (Number.EPSILON === undefined) {\r\n Number.EPSILON = Math.pow(2, -52);\r\n }\r\n if (Math.sign === undefined) {\r\n Math.sign = function(x) {\r\n return ((x \u003E 0) - (x \u003C 0)) || \u002Bx;\r\n };\r\n }\r\n return {\r\n // Decimal round (half away from zero)\r\n round: function(num, decimalPlaces) {\r\n var p = Math.pow(10, decimalPlaces || 0);\r\n var n = (num * p) * (1 \u002B Number.EPSILON);\r\n return Math.round(n) / p;\r\n },\r\n // Decimal ceil\r\n ceil: function(num, decimalPlaces) {\r\n var p = Math.pow(10, decimalPlaces || 0);\r\n var n = (num * p) * (1 - Math.sign(num) * Number.EPSILON);\r\n return Math.ceil(n) / p;\r\n },\r\n // Decimal floor\r\n floor: function(num, decimalPlaces) {\r\n var p = Math.pow(10, decimalPlaces || 0);\r\n var n = (num * p) * (1 \u002B Math.sign(num) * Number.EPSILON);\r\n return Math.floor(n) / p;\r\n },\r\n // Decimal trunc\r\n trunc: function(num, decimalPlaces) {\r\n return (num \u003C 0 ? this.ceil : this.floor)(num, decimalPlaces);\r\n },\r\n // Format using fixed-point notation\r\n toFixed: function(num, decimalPlaces) {\r\n return this.round(num, decimalPlaces).toFixed(decimalPlaces);\r\n }\r\n };\r\n})();\r\n\r\n// Solution 3\r\nvar DecimalPrecision3 = (function() {\r\n if (Math.trunc === undefined) {\r\n Math.trunc = function(v) {\r\n return v \u003C 0 ? Math.ceil(v) : Math.floor(v);\r\n };\r\n }\r\n var powers = [\r\n 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,\r\n 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,\r\n 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22\r\n ];\r\n var intpow10 = function(power) {\r\n /* Not in lookup table */\r\n if (power \u003C 0 || power \u003E 22) {\r\n return Math.pow(10, power);\r\n }\r\n return powers[power];\r\n };\r\n // Eliminate binary floating-point inaccuracies.\r\n var stripError = function(num) {\r\n if (Number.isInteger(num))\r\n return num;\r\n return parseFloat(num.toPrecision(15));\r\n };\r\n var decimalAdjust = function myself(type, num, decimalPlaces) {\r\n if (type === \u0027round\u0027 \u0026\u0026 num \u003C 0)\r\n return -myself(type, -num, decimalPlaces);\r\n var p = intpow10(decimalPlaces || 0);\r\n var n = stripError(num * p);\r\n return Math[type](n) / p;\r\n };\r\n return {\r\n // Decimal round (half away from zero)\r\n round: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces);\r\n },\r\n // Decimal ceil\r\n ceil: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027ceil\u0027, num, decimalPlaces);\r\n },\r\n // Decimal floor\r\n floor: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027floor\u0027, num, decimalPlaces);\r\n },\r\n // Decimal trunc\r\n trunc: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027trunc\u0027, num, decimalPlaces);\r\n },\r\n // Format using fixed-point notation\r\n toFixed: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces).toFixed(decimalPlaces);\r\n }\r\n };\r\n})();\r\n\r\n// Solution 4\r\nvar DecimalPrecision4 = (function() {\r\n if (Math.trunc === undefined) {\r\n Math.trunc = function(v) {\r\n return v \u003C 0 ? Math.ceil(v) : Math.floor(v);\r\n };\r\n }\r\n var powers = [\r\n 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,\r\n 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,\r\n 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22\r\n ];\r\n var intpow10 = function(power) {\r\n /* Not in lookup table */\r\n if (power \u003C 0 || power \u003E 22) {\r\n return Math.pow(10, power);\r\n }\r\n return powers[power];\r\n };\r\n var toPrecision = function(num, significantDigits) {\r\n // Return early for \u00B10, NaN and Infinity.\r\n if (!num || !Number.isFinite(num))\r\n return num;\r\n // Compute shift of the decimal point (sf - leftSidedDigits).\r\n var shift = significantDigits - 1 - Math.floor(Math.log10(Math.abs(num)));\r\n // Return if rounding to the same or higher precision.\r\n var decimalPlaces = 0;\r\n for (var p = 1; num != Math.round(num * p) / p; p *= 10) decimalPlaces\u002B\u002B;\r\n if (shift \u003E= decimalPlaces)\r\n return num;\r\n // Round to \u0022shift\u0022 fractional digits\r\n var scale = intpow10(Math.abs(shift));\r\n return shift \u003E 0 ?\r\n Math.round(num * scale) / scale :\r\n Math.round(num / scale) * scale;\r\n };\r\n // Eliminate binary floating-point inaccuracies.\r\n var stripError = function(num) {\r\n if (Number.isInteger(num))\r\n return num;\r\n return toPrecision(num, 15);\r\n };\r\n var decimalAdjust = function myself(type, num, decimalPlaces) {\r\n if (type === \u0027round\u0027 \u0026\u0026 num \u003C 0)\r\n return -myself(type, -num, decimalPlaces);\r\n var p = intpow10(decimalPlaces || 0);\r\n var n = stripError(num * p);\r\n return Math[type](n) / p;\r\n };\r\n return {\r\n // Decimal round (half away from zero)\r\n round: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces);\r\n },\r\n // Decimal ceil\r\n ceil: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027ceil\u0027, num, decimalPlaces);\r\n },\r\n // Decimal floor\r\n floor: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027floor\u0027, num, decimalPlaces);\r\n },\r\n // Decimal trunc\r\n trunc: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027trunc\u0027, num, decimalPlaces);\r\n },\r\n // Format using fixed-point notation\r\n toFixed: function(num, decimalPlaces) {\r\n return decimalAdjust(\u0027round\u0027, num, decimalPlaces).toFixed(decimalPlaces);\r\n }\r\n };\r\n})();\r\n","TestCases":[{"Name":"Solution 1 (Exponential notation)","Code":"/** Solution 1 (Exponential notation) **/\r\nDecimalPrecision.round(0.5, 0);\r\nDecimalPrecision.round(-0.5, 0);\r\nDecimalPrecision.ceil(1e-8, 2);\r\nDecimalPrecision.floor(1e-8, 2);\r\nDecimalPrecision.round(5.12, 1);\r\nDecimalPrecision.round(-5.12, 1);\r\nDecimalPrecision.ceil(5.12, 1);\r\nDecimalPrecision.ceil(-5.12, 1);\r\nDecimalPrecision.floor(5.12, 1);\r\nDecimalPrecision.floor(-5.12, 1);\r\nDecimalPrecision.trunc(5.12, 1);\r\nDecimalPrecision.trunc(-5.12, 1);\r\nDecimalPrecision.round(1.005, 2);\r\nDecimalPrecision.round(39.425, 2);\r\nDecimalPrecision.round(-1.005, 2);\r\nDecimalPrecision.round(-39.425, 2);\r\nDecimalPrecision.ceil(9.130, 2);\r\nDecimalPrecision.ceil(65.180, 2);\r\nDecimalPrecision.ceil(-2.260, 2);\r\nDecimalPrecision.ceil(-18.150, 2);\r\nDecimalPrecision.floor(2.260, 2);\r\nDecimalPrecision.floor(18.150, 2);\r\nDecimalPrecision.floor(-9.130, 2);\r\nDecimalPrecision.floor(-65.180, 2);\r\nDecimalPrecision.trunc(2.260, 2);\r\nDecimalPrecision.trunc(18.150, 2);\r\nDecimalPrecision.trunc(-2.260, 2);\r\nDecimalPrecision.trunc(-18.150, 2);\r\nDecimalPrecision.round(1262.48, -1);\r\nDecimalPrecision.round(1262.48, -2);","IsDeferred":false},{"Name":"Solution 2 (Number.EPSILON)","Code":"/** Solution 2 (Number.EPSILON) **/\r\nDecimalPrecision2.round(0.5, 0);\r\nDecimalPrecision2.round(-0.5, 0);\r\nDecimalPrecision2.ceil(1e-8, 2);\r\nDecimalPrecision2.floor(1e-8, 2);\r\nDecimalPrecision2.round(5.12, 1);\r\nDecimalPrecision2.round(-5.12, 1);\r\nDecimalPrecision2.ceil(5.12, 1);\r\nDecimalPrecision2.ceil(-5.12, 1);\r\nDecimalPrecision2.floor(5.12, 1);\r\nDecimalPrecision2.floor(-5.12, 1);\r\nDecimalPrecision2.trunc(5.12, 1);\r\nDecimalPrecision2.trunc(-5.12, 1);\r\nDecimalPrecision2.round(1.005, 2);\r\nDecimalPrecision2.round(39.425, 2);\r\nDecimalPrecision2.round(-1.005, 2);\r\nDecimalPrecision2.round(-39.425, 2);\r\nDecimalPrecision2.ceil(9.130, 2);\r\nDecimalPrecision2.ceil(65.180, 2);\r\nDecimalPrecision2.ceil(-2.260, 2);\r\nDecimalPrecision2.ceil(-18.150, 2);\r\nDecimalPrecision2.floor(2.260, 2);\r\nDecimalPrecision2.floor(18.150, 2);\r\nDecimalPrecision2.floor(-9.130, 2);\r\nDecimalPrecision2.floor(-65.180, 2);\r\nDecimalPrecision2.trunc(2.260, 2);\r\nDecimalPrecision2.trunc(18.150, 2);\r\nDecimalPrecision2.trunc(-2.260, 2);\r\nDecimalPrecision2.trunc(-18.150, 2);\r\nDecimalPrecision2.round(1262.48, -1);\r\nDecimalPrecision2.round(1262.48, -2);","IsDeferred":false},{"Name":"Solution 3 (Double rounding)","Code":"/** Solution 3 (Double rounding) **/\r\nDecimalPrecision3.round(0.5, 0);\r\nDecimalPrecision3.round(-0.5, 0);\r\nDecimalPrecision3.ceil(1e-8, 2);\r\nDecimalPrecision3.floor(1e-8, 2);\r\nDecimalPrecision3.round(5.12, 1);\r\nDecimalPrecision3.round(-5.12, 1);\r\nDecimalPrecision3.ceil(5.12, 1);\r\nDecimalPrecision3.ceil(-5.12, 1);\r\nDecimalPrecision3.floor(5.12, 1);\r\nDecimalPrecision3.floor(-5.12, 1);\r\nDecimalPrecision3.trunc(5.12, 1);\r\nDecimalPrecision3.trunc(-5.12, 1);\r\nDecimalPrecision3.round(1.005, 2);\r\nDecimalPrecision3.round(39.425, 2);\r\nDecimalPrecision3.round(-1.005, 2);\r\nDecimalPrecision3.round(-39.425, 2);\r\nDecimalPrecision3.ceil(9.130, 2);\r\nDecimalPrecision3.ceil(65.180, 2);\r\nDecimalPrecision3.ceil(-2.260, 2);\r\nDecimalPrecision3.ceil(-18.150, 2);\r\nDecimalPrecision3.floor(2.260, 2);\r\nDecimalPrecision3.floor(18.150, 2);\r\nDecimalPrecision3.floor(-9.130, 2);\r\nDecimalPrecision3.floor(-65.180, 2);\r\nDecimalPrecision3.trunc(2.260, 2);\r\nDecimalPrecision3.trunc(18.150, 2);\r\nDecimalPrecision3.trunc(-2.260, 2);\r\nDecimalPrecision3.trunc(-18.150, 2);\r\nDecimalPrecision3.round(1262.48, -1);\r\nDecimalPrecision3.round(1262.48, -2);","IsDeferred":false},{"Name":"Solution 4 (Double rounding v2)","Code":"/** Solution 4 (Double rounding v2) **/\r\nDecimalPrecision4.round(0.5, 0);\r\nDecimalPrecision4.round(-0.5, 0);\r\nDecimalPrecision4.ceil(1e-8, 2);\r\nDecimalPrecision4.floor(1e-8, 2);\r\nDecimalPrecision4.round(5.12, 1);\r\nDecimalPrecision4.round(-5.12, 1);\r\nDecimalPrecision4.ceil(5.12, 1);\r\nDecimalPrecision4.ceil(-5.12, 1);\r\nDecimalPrecision4.floor(5.12, 1);\r\nDecimalPrecision4.floor(-5.12, 1);\r\nDecimalPrecision4.trunc(5.12, 1);\r\nDecimalPrecision4.trunc(-5.12, 1);\r\nDecimalPrecision4.round(1.005, 2);\r\nDecimalPrecision4.round(39.425, 2);\r\nDecimalPrecision4.round(-1.005, 2);\r\nDecimalPrecision4.round(-39.425, 2);\r\nDecimalPrecision4.ceil(9.130, 2);\r\nDecimalPrecision4.ceil(65.180, 2);\r\nDecimalPrecision4.ceil(-2.260, 2);\r\nDecimalPrecision4.ceil(-18.150, 2);\r\nDecimalPrecision4.floor(2.260, 2);\r\nDecimalPrecision4.floor(18.150, 2);\r\nDecimalPrecision4.floor(-9.130, 2);\r\nDecimalPrecision4.floor(-65.180, 2);\r\nDecimalPrecision4.trunc(2.260, 2);\r\nDecimalPrecision4.trunc(18.150, 2);\r\nDecimalPrecision4.trunc(-2.260, 2);\r\nDecimalPrecision4.trunc(-18.150, 2);\r\nDecimalPrecision4.round(1262.48, -1);\r\nDecimalPrecision4.round(1262.48, -2);","IsDeferred":false}]}