The most copied StackOverflow snippet of all time is flawed (2019)
Coincidentally I had to write this today, and perplexingly none of the answers on that popular question seem to be as efficient and readable as what I wrote:<p><pre><code>  function prettyPrintBytesSI(x: number): string {
    const magnitude = Math.abs(x)
    if (magnitude < 1e3) return `${x} B`
    else if (magnitude < 1e6) return `${x/1e3} kB`
    else if (magnitude < 1e9) return `${x/1e6} MB`
    else if (magnitude < 1e12) return `${x/1e9} GB`
    else if (magnitude < 1e15) return `${x/1e12} TB`
    else if (magnitude < 1e18) return `${x/1e15} PB`
    else return `${x} B`
  }</code></pre>