[javascript] Converting file size in bytes to human-readable string

A simple and short "Pretty Bytes" function for the SI system without the unnecessary fractionals rounding.

In fact, because the number size is supposed to be human-readable, a "1 of a thousand fraction" display is no longer human.

The number of decimal places is defaulted to 2 but can be modified on calling the function to other values. The common mostly display is the default 2 decimal place.

The code is short and uses the method of Number String Triplets.

Hope it is useful and a good addition to the other excellent codes already posted here.

_x000D_
_x000D_
// Simple Pretty Bytes with SI system
// Without fraction rounding

function numberPrettyBytesSI(Num=0, dec=2){
if (Num<1000) return Num+" Bytes";
Num =("0".repeat((Num+="").length*2%3)+Num).match(/.{3}/g);
return Number(Num[0])+"."+Num[1].substring(0,dec)+" "+"  kMGTPEZY"[Num.length]+"B";
}

console.log(numberPrettyBytesSI(0));
console.log(numberPrettyBytesSI(500));
console.log(numberPrettyBytesSI(1000));
console.log(numberPrettyBytesSI(15000));
console.log(numberPrettyBytesSI(12345));
console.log(numberPrettyBytesSI(123456));
console.log(numberPrettyBytesSI(1234567));
console.log(numberPrettyBytesSI(12345678));
_x000D_
_x000D_
_x000D_