Pretty-Print Number of Bytes

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of pretty-printing numbers in bytes to a human-readable format using JavaScript. We will learn how to convert a given number of bytes into KB, MB, GB, TB, PB, EB, ZB, and YB by using an array dictionary of units. With the help of the Number.prototype.toPrecision() method, we will truncate the number to a certain number of digits and build a prettified string to display the result.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") subgraph Lab Skills javascript/variables -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} javascript/data_types -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} javascript/arith_ops -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} javascript/comp_ops -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} javascript/cond_stmts -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} javascript/array_methods -.-> lab-28554{{"`Pretty-Print Number of Bytes`"}} end

Convert Bytes to Human-Readable String

To convert a number in bytes to a human-readable string, use the prettyBytes() function. Here are some things to keep in mind:

  • The function uses an array dictionary of units to be accessed based on the exponent.
  • You can use the second argument, precision, to truncate the number to a certain number of digits. The default value is 3.
  • You can use the third argument, addSpace, to add space between the number and unit. The default value is true.
  • The function returns the prettified string by building it up, taking into account the supplied options and whether it is negative or not.

Here's the code for the prettyBytes() function:

const prettyBytes = (num, precision = 3, addSpace = true) => {
  const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  if (Math.abs(num) < 1) return num + (addSpace ? " " : "") + UNITS[0];
  const exponent = Math.min(
    Math.floor(Math.log10(num < 0 ? -num : num) / 3),
    UNITS.length - 1
  );
  const n = Number(
    ((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision)
  );
  return (num < 0 ? "-" : "") + n + (addSpace ? " " : "") + UNITS[exponent];
};

And here are some examples of using the prettyBytes() function:

prettyBytes(1000); // '1 KB'
prettyBytes(-27145424323.5821, 5); // '-27.145 GB'
prettyBytes(123456789, 3, false); // '123MB'

Summary

Congratulations! You have completed the Pretty-Print Number of Bytes lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like