{"id":1004,"date":"2012-03-31T19:53:33","date_gmt":"2012-04-01T01:53:33","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1004"},"modified":"2012-03-31T20:16:38","modified_gmt":"2012-04-01T02:16:38","slug":"reimplementation-of-number-prototype-tofixed","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/03\/reimplementation-of-number-prototype-tofixed\/","title":{"rendered":"Reimplementation of Number.prototype.toFixed"},"content":{"rendered":"<p>Number.prototype.toFixed was giving me too many problems, so I just rewrote it.<br \/>\nThere&#8217;s the source.<\/p>\n<p><code><\/p>\n<pre lang='javascript'>\r\n\/**\r\n@author Larry Battle <a alt=\"contact me\" href=\"bateru.com\/news\/contact\">Contact Me<\/a>\r\n@date\tMar 30, 2012\r\n@purpose Provide the fix for Number.prototype.toFixed() function.\r\n@info source at http:\/\/bateru.com\/news\/2012\/03\/reimplementation-of-number-prototype-tofixed\r\n *\/\r\nNumber.prototype.toFixed_fix = function (precision) {\r\n\tvar num = this.toString(), zeros = '00000000000000000000', newNum, decLength, factor;\r\n\tif (0 > precision || precision > 20) {\r\n\t\tthrow new RangeError(\"toFixed() digits argument must be between 0 and 20\");\r\n\t}\r\n\tif (Math.abs(num) === Infinity || Math.abs(num) >= 1e21) {\r\n\t\treturn num;\r\n\t}\r\n\tprecision = parseInt(precision, 10) || 0;\r\n\tnewNum = num = isNaN(parseInt(num, 10)) ? '0' : num;\r\n\tif (\/\\.\/.test(num)) {\r\n\t\tdecLength = num.split('.')[1].length;\r\n\t\tif (decLength < precision) {\r\n\t\t\tnewNum += zeros.substring(0, precision - decLength);\r\n\t\t} else {\r\n\t\t\tfactor = Math.pow(10, precision);\r\n\t\t\tnewNum = Math.round(num * factor) \/ factor;\r\n\t\t}\r\n\t} else {\r\n\t\tif (precision) {\r\n\t\t\tnewNum += '.' + zeros.substring(0, precision);\r\n\t\t}\r\n\t}\r\n\treturn newNum;\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p>Test cases <a href=\"http:\/\/bateru.com\/projects\/number.prototype.tofix\/\">here<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/qunit-number.prototype.tofixed2.jpg\" alt=\"\" title=\"qunit number.prototype.tofixed\" width=\"550\" height=\"392\" class=\"aligncenter size-full wp-image-1014\" srcset=\"https:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/qunit-number.prototype.tofixed2.jpg 550w, https:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/qunit-number.prototype.tofixed2-300x213.jpg 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>Want to learn more about Javascript?<br \/>\nCheckout this 'Professional JavaScript for Web Developers'.<br \/>\n<iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=baterucom-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=1118026691&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Number.prototype.toFixed was giving me too many problems, so I just rewrote it. There&#8217;s the source. \/** @author Larry Battle Contact Me @date Mar 30, 2012 @purpose Provide the fix for Number.prototype.toFixed() function. @info source at http:\/\/bateru.com\/news\/2012\/03\/reimplementation-of-number-prototype-tofixed *\/ Number.prototype.toFixed_fix = function (precision) { var num = this.toString(), zeros = &#8216;00000000000000000000&#8217;, newNum, decLength, factor; if (0 > &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/03\/reimplementation-of-number-prototype-tofixed\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Reimplementation of 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,10],"tags":[],"class_list":["post-1004","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1004","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=1004"}],"version-history":[{"count":7,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":1015,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1004\/revisions\/1015"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}