Nodejs Number to Readable Format humanize(rounding, delimiter, separator)

Here you can find the source of humanize(rounding, delimiter, separator)

Method Source Code

Number.prototype.humanize = function(rounding, delimiter, separator) {
    rounding = (typeof rounding != 'undefined') ?  rounding : 2;
    delimiter = (typeof delimiter != 'undefined') ? delimiter : ',';
    separator = (typeof separator != 'undefined') ? separator : '.';

    var value = (function(value) {
        if (rounding == 0) return Math.round(value);
        var round_by = Math.pow(10, rounding);
        return (Math.round(value * (round_by)) / round_by);
    })(this);//from w w  w.  ja  va 2 s  .c o  m

    parts = value.toString().split('.');
    parts[0] = parts[0].gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "#{1}" + delimiter);
    return parts.join(separator);
};

Related

  1. toEnglish()
    var numbersToWords = {
      0: 'zero',
      1: 'one',
      2: 'two',
      3: 'three',
      4: 'four',
      5: 'five',
      6: 'six',
      7: 'seven',
    ...
    
  2. to_kilometers()
    Number.prototype.to_kilometers = function() {
      return this / 1000.0;
    };
    
  3. to_kilometers_per_hour()
    Number.prototype.to_kilometers_per_hour = function() {
      return this / 1000.0 * 3600.0;
    };
    
  4. to_miles()
    Number.prototype.to_miles = function() {
      return this / 1609.344;
    };
    
  5. to_miles_per_hour()
    Number.prototype.to_miles_per_hour = function() {
      return this / 1609.344 * 3600.0;
    };
    
  6. toThousands()
    Number.prototype.toThousands = function() {
      return (this / 1000).toFixed(1) + 'K';
    };
    
  7. byteFormat()
    Number.prototype.byteFormat = function(){
      var str = ""
      if(this < 1024){
        str = this + "??"
      } else if(this < 1024*2014){
        str = (this/1024).toFixed(1) + "KB"
      } else{
        str = this/(1024*1024).toFixed(1) + "MB"
      return str
    
  8. bytesToHuman()
    Number.prototype.bytesToHuman = function () {
      return (this/(1000*1000)).toFixed(2) + 'Mb';
    };
    
  9. bytesToSize()
    Number.prototype.bytesToSize = function() {
      var bytes = this;
      var k = 1024;
      var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      if (bytes == 0) return '0 Bytes';
      var i = parseInt(Math.floor(Math.log(bytes) / Math.log(k)),10);
      return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
    };