JavaScript | Convert bytes to human-readable string
Given the size of a file(in Bytes), The task is to convert it into human-readable form using JavaScript. Here are few methods discussed.
Example 1: This example converts the file Size(in Bytes) into human-readable form. It shows the values in decimal and for less than 1024 Bytes, it remains in Bytes.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Converting bytes to human-readable string. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = 'Convert bytes to human readable form'; var down = document.getElementById('GFG_DOWN'); size = function(bytes) { if (bytes == 0) { return "0.00 B"; } var e = Math.floor(Math.log(bytes) / Math.log(1024)); return (bytes / Math.pow(1024, e)).toFixed(2) + ' ' + ' KMGTP'.charAt(e) + 'B'; } function GFG_Fun() { var bytes = 2007777777770; down.innerHTML = bytes + " bytes = " + size(bytes); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example converts the file Size(in Bytes) into human-readable form. It shows the values in decimal and for less than 1024 Bytes, it remains in Bytes. But, with a different approach.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Converting bytes to human-readable string. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = 'Convert bytes to human readable form'; var down = document.getElementById('GFG_DOWN'); function getSize(size) { var sizes = [' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']; for (var i = 1; i < sizes.length; i++) { if (size < Math.pow(1024, i)) return (Math.round((size / Math.pow( 1024, i - 1)) * 100) / 100) + sizes[i - 1]; } return size; } function GFG_Fun() { var bytes = 1024; down.innerHTML = bytes + " bytes = " + getSize(bytes); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- Convert string into date using JavaScript
- JavaScript | Convert a string to boolean
- How to convert an object to string using JavaScript?
- How to convert string into float in JavaScript?
- Convert a string to an integer in JavaScript
- How to convert string to camel case in JavaScript ?
- Convert string to title case in JavaScript
- Convert comma separated string to array using JavaScript
- How to convert a currency string to a double value with jQuery or Javascript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
- How to Convert Array to Set in JavaScript?
- How to convert decimal to hex in JavaScript ?
- How to convert Set to Array in JavaScript?
- How to convert a JavaScript date to UTC?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



