javascrpt formatBytes

function niceBytes(x){
 const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  let l = 0, n = parseInt(x, 10) || 0;
  while(n >= 1024 && ++l){
      n = n/1024;
  }
  return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
}

5
1
Awgiedawgie 440220 points

                                    function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0 Bytes';

    const k = 1024;
    const dm = decimals &lt; 0 ? 0 : decimals;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    const i = Math.floor(Math.log(bytes) / Math.log(k));

    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
javascript beautifier js bytes to mbps convert bit into KB in angylar convert file size from MB to KB in angular javascript bytes to kb javascript size in kb kb to mb in js byte to mb converter in jquery javascript number for one gigabyte convert bytes to mb using javascript formula for bytes to mb javascript discord.js bytes to Mbps discord.js display bytes as megabytes byte to mb javascript kbyte to mb javascript angular byte to mb transform count into KB kb to mb to gb jquery kb to md to gb jquery how convert mb to bytes in node convert mb to bytes binary javascript program to convert bits to bytes js js byte to mbps js format bytes bytes in gigabyte js converter bytes to mb node mb to bytes javascript javascript convert bytes to terabytes convert kilobyte to megabyte typescript javascript function to convert bytes to megabytes convert b' to javascript check value in kb nodejs check value in kb javascript convert mb to bytes javascript integer to mega convert js converting bytes to megabytes typescript bytes to kb mb gb javascript how to get byte to kb or mb in javascript jquery convert KB to MB bytes to megabytes javascript bytes to kilobytes javascript format bytes javascript Math.floor(val / 1024) for mb react convert gb to bytes bytes converter javascript convert bytes to megabytes, js bytes to mb formula javascript javascript get unit of bytes jquery bytes to mb javascript byte to kilobytes ts convert file size to kb how to display number in kb angular js convert bytes to kb, mb or gb? how to convert bytes into mb in angular how to convert bytes into mb in angular' size mb string to byte js convert string to byte size mb js mb string size to kb converter js kb to mb javascript how to convert kilobytes to mb in typescript how to convert bytes to mb in typescript bytes to mb angular how to covert file size byte to kb and mb to show in vue js how to covert file size byte to kb and mb to show in vuejs convert bytes to kilobytes js how to convert image size from bytes to mb typescript how to convert image size from kb to mb typescript jquery convert byte to TiB jquery convert byte to gibibyte convert bytes to gb in javascript file size calculation based on value to show in kb/mb in javascript convert kb to mb javascript jquery math convert byte to MB jquery conversion byte to kb mb gb jquery byte to megabyte typescript function bytes to mb convert convert bytes to mb typescript function convert bytes to mb typescript javascript function convert bytes into mb convert bytes into gb nodejs javascript code to convert byte to mb convert byte to mb javascript bytes to kb javascript filesize javascript kb js bit to kb mb convert bytes to mb in react octoet to mb nodejs bytes to unit converter javascript javascript convert kb to bytes javascript convert gb to bytes javascript convert size value to byte value javascript convert file size to mb javascript take value and size unit and convert to bytes convert bytes to megabytes javascript convert bits to mb javascript kb to mb converter javascript javascript file size in mb extract value from kb mb gb tb javascript convert kb to gb in javascript convert byte to megabyte javascript javascript convert bits to megabytes javascript convert mega to bit javascript convert bit to megabit javascript converter bit to megabit javascript converter bit to mega 1000000 bytes in 1mb in javascript how many bytes in 1mb in javascript how to convert kb to mb in javascript bytes to gb convert using javascript bytes to mb js javascript bytes to mb convertBytesToSize in angular blob size in mb javascript 1kb to bytes js 2mb in kb js javascript calculate bytes to mb jquery simple byte convert jquery simple byte convertor convert bytes to kb mb gb javascript node bytes to mb javascript bytes to kb and mb js converting byte to kb javascript convert bytes to mb convert bytes to kb in javascript print byte to kb javascript bytes to mb javascript javascript convert bytes to tb formula how to convert bytes to mb in javascript node js size to readable format byte to md in js javascript convert bytes to GB convert byte to mb in javascript javascript bytes to human readable calculate bytes of file js convert mb to gb js javascript file size converter javascript byte to mb byte to MB js combertir bytes en mb js convert bytes to mb javascript formatBytes node convert number of bytes to KB, MB, GB javascript js file size in kb js function for converting bytes to size js byte to size js convert from bytes to gb how to convert bytes to megabytes javascript javascript convert mb to bytes convert bytes to mb function js js convert bytes to mb convert kb to bytes in javascript javascript convert bytes to kilobytes nodejs convert file to kb/mb size convert image size to kb in javascript 1 mb in javascript js bytes to mb vue convert kb to mb js convert kb to mb javascript bytes to human readable size convert bytes to mb js gb to byte nodejs mi to gb nodejs bytes to gb javascript convert file size to kb javascript js convert integer to gb javascrpt formatBytes
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source