{"id":711,"date":"2011-08-24T16:58:46","date_gmt":"2011-08-24T22:58:46","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=711"},"modified":"2012-03-27T20:01:43","modified_gmt":"2012-03-28T02:01:43","slug":"code-of-the-day-javascript-convert-bytes-to-kb-mb-gb-etc","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/08\/code-of-the-day-javascript-convert-bytes-to-kb-mb-gb-etc\/","title":{"rendered":"Code of the day: Javascript convert bytes to KB, MB, GB, etc"},"content":{"rendered":"<p>Note: This uses the IEC standard. That means 1 KB = 1024, not 1000 like the SI standard.<\/p>\n<pre lang='javascript'>\r\n\/**\r\n* @function: getBytesWithUnit()\r\n* @purpose: Converts bytes to the most simplified unit.\r\n* @param: (number) bytes, the amount of bytes\r\n* @returns: (string)\r\n*\/\r\nvar getBytesWithUnit = function( bytes ){\r\n\tif( isNaN( bytes ) ){ return; }\r\n\tvar units = [ ' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB' ];\r\n\tvar amountOf2s = Math.floor( Math.log( +bytes )\/Math.log(2) );\r\n\tif( amountOf2s < 1 ){\r\n\t\tamountOf2s = 0;\r\n\t}\r\n\tvar i = Math.floor( amountOf2s \/ 10 );\r\n\tbytes = +bytes \/ Math.pow( 2, 10*i );\r\n\t\r\n\t\/\/ Rounds to 3 decimals places.\r\n        if( bytes.toString().length > bytes.toFixed(3).toString().length ){\r\n            bytes = bytes.toFixed(3);\r\n        }\r\n\treturn bytes + units[i];\r\n};\r\n<\/pre>\n<p><b>Output:<\/b><\/p>\n<pre lang='javascript'>\r\nconsole.log( getBytesWithUnit( ) );\t\t\t\/\/ returns undefined.\r\nconsole.log( getBytesWithUnit( 'non a number.' ) );\t\/\/ returns undefined.\r\nconsole.log( getBytesWithUnit( '123' ));\t\t\/\/ returns '123 bytes'.\r\nconsole.log( getBytesWithUnit( 1024 ));\t\t\/\/ returns '1 KB'.\r\nconsole.log( getBytesWithUnit( (1024 * 1024) + 1024 )); \/\/ returns '1.001 MB'.\r\nconsole.log( getBytesWithUnit( 1024 * 1024 * 1024 )); \/\/ returns '1 GB'.\r\nconsole.log( getBytesWithUnit( 1024 * 1024 * 64 )); \/\/ returns '64 MB'.\r\n<\/pre>\n<p><b>Update<\/b><br \/>\nI updated the script to support both the SI and IEC standard.<br \/>\nCheck it out here <a href=\"http:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-converts-bytes-to-unit\/\">http:\/\/bateru.com\/news\/2012\/03\/code-of-the-day-converts-bytes-to-unit\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: This uses the IEC standard. That means 1 KB = 1024, not 1000 like the SI standard. \/** * @function: getBytesWithUnit() * @purpose: Converts bytes to the most simplified unit. * @param: (number) bytes, the amount of bytes * @returns: (string) *\/ var getBytesWithUnit = function( bytes ){ if( isNaN( bytes ) ){ return; &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/08\/code-of-the-day-javascript-convert-bytes-to-kb-mb-gb-etc\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Javascript convert bytes to KB, MB, GB, etc<\/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,23,10],"tags":[30,164],"class_list":["post-711","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-math","category-tutorials","tag-code-of-the-day","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/711","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=711"}],"version-history":[{"count":8,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/711\/revisions"}],"predecessor-version":[{"id":716,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/711\/revisions\/716"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}