Skip to content Skip to sidebar Skip to footer

How To Write This Js Function In Best(smartest) Way?

I would like to write the function which takes file size in bytes(7762432) and return value in user readable format (7,762,432 bytes). function(fileSizeBytes){ // mind blowing

Solution 1:

functionaddCommas(value) {
    var regex = /^(\d+)(\d{3}[\.]?.*)$/;
    while (regex.exec(value)) {
        value= RegExp.$1
                ? RegExp.$1 + ',' + RegExp.$2
                : RegExp.$2;
    }
    return value;
}

Solution 2:

one line function:

functionformatFileSizeBytes(size) {
    return size.replace(/(\d)(?=(\d{3})+$)/g, "$1,");
}

some testing:

for (var i = 0; i <= "123456789".split('').length; i++) {  
    alert(formatFileSizeBytes(a.slice(0,i).join(''))); 
}

Solution 3:

One liner:

function(fileSizeBytes){
    return fileSizeBytes.toString(10).split('').reverse().join('').replace(/(...)/g,'$1,').replace(/,$/,'').split('').reverse().join('') + ' bytes';
}

Solution 4:

Ok, there should be some ENTERPRISE solution for this with intensive i18n support

var numberLocaleFormat = (function() {
  var cache = {};
  return (function (separator, grouping, groupingAdd) {
    separator = separator || ",";
    grouping = grouping || 3;
    groupingAdd = groupingAdd || grouping;
    var key = grouping + "+" + groupingAdd + "/" + separator;
    if (key in cache) return cache[key];
    return (cache[key] = function(number){
      var str = [], number = number.toString();
      var separatorIndex = grouping;
      for (var i = number.length-1, idx = 0; i >= 0; i--, idx++) {
        str.push(number.charAt(i));
        if ((idx == separatorIndex-1) && i) {
           separatorIndex += groupingAdd;
           str.push(separator);
        }
      }
      return str.reverse().join('');
    });
  });
})();

Usage:

numberLocaleFormat()(1234578); // "12,345,678", AmericanumberLocaleFormat(" ")(12345678); // "12 345 678", EuropenumberLocaleFormat(",",4)(12345678); // "1234,5678", JapannumberLocaleFormat(",", 3, 2)(12345678); // "1,23,45,678", India

Solution 5:

I wrote this function four years ago:

function formatNumber(x) { 
    if(!isFinite(x)) return x.toString();
    var str         = Math.abs(x).toString();
    if(str.indexOf("e") != -1) return x.toString();
    var decimalPos  = str.indexOf(".");
    vardecimal;

    if(decimalPos == -1)
        decimal = "";
    else {
        decimal = str.substring(decimalPos);
        str = str.substring(0, decimalPos);
    }
    if(str.length < 4) return x.toString();

    var start = str.substr(0, str.length % 3);
    if(start.length != 0) {
        str = str.substr(start.length)
        start += ",";
    }
    if(x < 0) start = "-" + start;

    var segments = new Array(Math.ceil(str.length / 3.0));
    for(var i = 0; i < str.length; i += 3) {
        segments[i / 3] = str.substr(i, 3);
    }

    return start + segments.join() + decimal;
};

Post a Comment for "How To Write This Js Function In Best(smartest) Way?"