This is a simple and short "Pretty Bytes" javascript function using the SI Decimal System for quantifying bytes.
The code does not use complex maths such as Math.pow() or Math.log() and mainly uses strings with an array.
The function uses the SI system of decimals as shown below (1 kilo = 1,000, 1 M = 1,000,000, etc.).
The number of decimal places is defaulted to 2 (no rounding is necessary) but can be modified on calling the function to other values. The common most used display is the default 2 decimal place.
The code is short and uses the method of Number String Triplets to first convert the input number (the integer) into a Number String Triplet, then into an array of triplets.
The following one-line code is used for converting an integer into a Number String Triplet so that the resulting string will always be made of multiple of 3's by padding zero(s) to the left. It is the same concept used under this code review article: Simple Number to Words using a Single Loop String Triplets
Num = "0".repeat((Num += "").length * 2 % 3) + Num; // Make a Number String Triplet
The output is then made up of concatenating the first element of the array being the whole number and the 2nd element being the fraction (after chopping off for decimals) and adding the suffix letter.
Numbers below 1000 do not require the functional code and are cleared out first.
If there is a possibility that the input number may be a float, it should be converted to an integer first Num = Math.floor(Num); before passing it to the function.
As the code is mainly for displaying size, no code is added for handling negative numbers or floats (not sure if one would give a size as '3 and a half byte').
I welcome your code review.
Thanks
Mohsen Alyafei
/*********************************************************************
* @function : numberPrettyBytesSI()
* @purpose : Convert number bytes size to human readable format
* using the SI Decimal System.
* @version : 1.00
* @author : Mohsen Alyafei
* @date : 01 July 2020
* @param : {num} [integer] Number to be converted
* @param : {decimals} [integer] Deciaml places (defaul 2)
* @returns : {String} Pretty Number
**********************************************************************/
function numberPrettyBytesSI(Num=0, decimals=2){
if (Num < 1000) return Num + " Bytes";
Num = "0".repeat((Num += "").length * 2 % 3) + Num; // Make a Number String Triplet
Num = Num.match(/.{3}/g); // Make an Array of Triplets
return Number(Num[0]) + // Whole Number withou leading zeros
"." + // dot
Num[1].substring(0, decimals) + " " + // Fractional part
" kMGTPEZY"[Num.length] + "B"; // The SI suffix
}
//*********** tests ***********************
console.log(numberPrettyBytesSI(0)); // 0 Bytes
console.log(numberPrettyBytesSI(500)); // 500 Bytes
console.log(numberPrettyBytesSI(1000)); // 1.00 kB
console.log(numberPrettyBytesSI(15000)); // 15.00 kB
console.log(numberPrettyBytesSI(12345)); // 12.34 Kb
console.log(numberPrettyBytesSI(123456)); // 123.45 kb
console.log(numberPrettyBytesSI(1234567)); // 1.23 MB
console.log(numberPrettyBytesSI(12345678)); // 12.34 MB
console.log(numberPrettyBytesSI(123456789)); // 123.45 MB
console.log(numberPrettyBytesSI(1234567890)); // 1.23 GB
console.log(numberPrettyBytesSI(1234567890,1)); // 1.2 GB
console.log(numberPrettyBytesSI(1234567890,3)); // 1.234 GB
