Nodejs Number Format formatHashrate(rate)

Here you can find the source of formatHashrate(rate)

Method Source Code

// ======================================================================
// formats a given hashrate (H/s) to humand readable hashrate
// like xxx.yyy GH/s
// ======================================================================
var formatHashrate = function(rate) {
    rate = parseFloat(rate); //  ww  w  . j  a  v  a 2s  .com
    unit = 'H/s';
    if (rate >= 1000) { rate /= 1000; unit = 'KH/s'; }
    if (rate >= 1000) { rate /= 1000; unit = 'MH/s'; }
    if (rate >= 1000) { rate /= 1000; unit = 'GH/s'; }
    if (rate >= 1000) { rate /= 1000; unit = 'TH/s'; }
    if (rate >= 1000) { rate /= 1000; unit = 'PH/s'; }
    
    return (rate.toFixed(2) + ' ' + unit);
}

Related

  1. withCommas()
    Number.prototype.withCommas = function() {
        return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    };
    
  2. withCommas()
    Number.prototype.withCommas = function() {
      var x = 3;
      var y = parseFloat(this).toString().reverse();
      while (x < y.length) {
        y = y.substring(0, x) + "," + y.substring(x); 1
        x += 4;
      return y.reverse();
    };
    ...
    
  3. withCommas()
    Number.prototype.withCommas = function() {
      var num = this;
      return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    
  4. addTrailingZero()
    Number.prototype.addTrailingZero = function(){
      if(this < 10)  return "0"+this;
      else  return this;
    };