{"id":972,"date":"2012-03-27T19:54:34","date_gmt":"2012-03-28T01:54:34","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=972"},"modified":"2012-03-27T20:05:23","modified_gmt":"2012-03-28T02:05:23","slug":"code-of-the-day-converts-bytes-to-unit","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-converts-bytes-to-unit\/","title":{"rendered":"Code of the day: Converts Bytes to Simplify Units"},"content":{"rendered":"<p>A few days ago I noticed that my <a href=\"http:\/\/bateru.com\/news\/2011\/08\/code-of-the-day-javascript-convert-bytes-to-kb-mb-gb-etc\/\">&#8220;code of the day&#8221;<\/a> article was wrong. So I spent some time fixing it and provided test cases to check my work.<br \/>\nThe main difference about this script is that it allows you to support both the SI and IEC standard and fixes a few rounding errors.<\/p>\n<p><a href=\"http:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/bytes.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/bytes.jpg\" alt=\"\" title=\"bytes\" width=\"296\" height=\"303\" class=\"aligncenter size-full wp-image-985\" srcset=\"https:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/bytes.jpg 296w, https:\/\/bateru.com\/news\/wp-content\/uploads\/2012\/03\/bytes-293x300.jpg 293w\" sizes=\"auto, (max-width: 296px) 100vw, 296px\" \/><\/a><\/p>\n<p><code><\/p>\n<pre lang='javascript'>\r\n\/\/ function: getBytesWithUnit\r\n\/\/ input: bytes (number)\r\n\/\/ input: useSI (boolean), if true then uses SI standard (1KB = 1000bytes), otherwise uses IEC (1KiB = 1024 bytes)\r\n\/\/ input: precision (number), sets the maximum length of decimal places.\r\n\/\/ input: useSISuffix (boolean), if true forces the suffix to be in SI standard. Useful if you want 1KB = 1024 bytes\r\n\/\/ returns (string), represents bytes is the most simplified form.\r\nvar getBytesWithUnit = function (bytes, useSI, precision, useSISuffix) {\r\n\t\"use strict\";\r\n\tif (!(!isNaN(bytes) && +bytes > -1 && isFinite(bytes))) {\r\n\t\treturn false;\r\n\t}\r\n\tvar units, obj,\tamountOfUnits, unitSelected, suffix;\r\n\tunits = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];\r\n\tobj = {\r\n\t\tbase : useSI ? 10 : 2,\r\n\t\tunitDegreeDiff : useSI ? 3 : 10\r\n\t};\r\n\tamountOfUnits = Math.max(0, Math.floor(Math.round(Math.log(+bytes) \/ Math.log(obj.base) * 1e6) \/ 1e6));\r\n\tunitSelected = Math.floor(amountOfUnits \/ obj.unitDegreeDiff);\r\n\tunitSelected = units.length > unitSelected ? unitSelected : units.length - 1;\r\n\tsuffix = (useSI || useSISuffix) ? units[unitSelected] : units[unitSelected].replace('B', 'iB');\r\n\tbytes = +bytes \/ Math.pow(obj.base, obj.unitDegreeDiff * unitSelected);\r\n\tprecision = precision || 3;\r\n\tif (bytes.toString().length > bytes.toFixed(precision).toString().length) {\r\n\t\tbytes = bytes.toFixed(precision);\r\n\t}\r\n\treturn bytes + \" \" + suffix;\r\n};\r\n\r\n<\/pre>\n<p><\/code><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/LOLIR4LwJPQ\" frameborder=\"0\" allowfullscreen><\/iframe><br \/>\n<br \/>\n<a href=\"http:\/\/bateru.com\/projects\/simplify_bytes\/index.html\"\/><b>Demo<\/b>: Test Script here<\/a><br \/>\n<br \/>\nWant to learn more about Javascript?<br \/>\nCheck out this &#8220;Professional JavaScript for Web Developers&#8221;.<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>A few days ago I noticed that my &#8220;code of the day&#8221; article was wrong. So I spent some time fixing it and provided test cases to check my work. The main difference about this script is that it allows you to support both the SI and IEC standard and fixes a few rounding errors. &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-converts-bytes-to-unit\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Converts Bytes to Simplify Units<\/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":[10],"tags":[30,164,166],"class_list":["post-972","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-code-of-the-day","tag-javascript","tag-math"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/972","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=972"}],"version-history":[{"count":10,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/972\/revisions"}],"predecessor-version":[{"id":984,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/972\/revisions\/984"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}