{"id":991,"date":"2012-03-29T02:47:23","date_gmt":"2012-03-29T08:47:23","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=991"},"modified":"2012-03-31T19:59:09","modified_gmt":"2012-04-01T01:59:09","slug":"code-of-the-day-fix-for-number-prototype-tofixed","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-fix-for-number-prototype-tofixed\/","title":{"rendered":"Code of the day: Fix for Number.prototype.toFixed"},"content":{"rendered":"<p>In javascript, Number.prototype.toFixed has problems rounding.<br \/>\nHere&#8217;s the fix and some test cases.<\/p>\n<p><code><\/p>\n<pre lang='javascript'>\r\nNumber.prototype.toFixed = (function () {\r\n\tvar oldToFixed = Number.prototype.toFixed;\r\n\treturn function (precision) {\r\n\t\tvar value = this.toString(), power = Math.pow(10, precision || 0);\r\n\t\treturn oldToFixed.call((Math.round(value * power) \/ power), precision);\r\n\t};\r\n}());\r\n<\/pre>\n<p><\/code><br \/>\nHere&#8217;s a simple test.<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n\/\/ Programmer: Larry Battle\r\n\/\/ Link: http:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-fix-for-number-prototype-tofixed\r\n(function (root) {\r\n\t\"use strict\";\r\n\tvar log = function( str ){\r\n\t\troot.document.body.innerHTML += \"log: \" + str + \"<br\/>\";\r\n\t},\r\n\tassert = function (a, b ) {\r\n\t\tvar message = a + ' !== ' + b;\r\n\t\tmessage += ( a === b ) ? \"=> true\" : \"=> false\";\r\n\t\tlog( message );\r\n\t};\r\n\tassert((0.595).toFixed(2), \"0.59\");\r\n\tassert((0.9).toFixed(0), \"1\");\r\n\tNumber.prototype.toFixed = (function () {\r\n\t\tvar oldToFixed = Number.prototype.toFixed;\r\n\t\treturn function (precision) {\r\n\t\t\tvar value = this.toString(), power = Math.pow(10, precision || 0);\r\n\t\t\treturn oldToFixed.call((Math.round(value * power) \/ power), precision);\r\n\t\t};\r\n\t}\r\n\t\t());\r\n\tlog(\"Applying the toFixed fix.\");\r\n\tassert((0.595).toFixed(2), \"0.60\");\r\n\tassert((0.9).toFixed(0), \"1\");\r\n}(this));\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Demo<\/b>: <a href=\"http:\/\/jsbin.com\/ijogus\/2\/edit\">Link<\/a><\/p>\n<p><b>Update!<\/b><br \/>\n<a href=\"http:\/\/bateru.com\/news\/2012\/03\/reimplementation-of-number-prototype-tofixed\/\">Improved code here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In javascript, Number.prototype.toFixed has problems rounding. Here&#8217;s the fix and some test cases. Number.prototype.toFixed = (function () { var oldToFixed = Number.prototype.toFixed; return function (precision) { var value = this.toString(), power = Math.pow(10, precision || 0); return oldToFixed.call((Math.round(value * power) \/ power), precision); }; }()); Here&#8217;s a simple test. \/\/ Programmer: Larry Battle \/\/ Link: &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-fix-for-number-prototype-tofixed\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Fix for Number.prototype.toFixed<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[129,123,164,166],"class_list":["post-991","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-bug","tag-error","tag-javascript","tag-math"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":6,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":1007,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/991\/revisions\/1007"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}