Nodejs Number Calculate countDecimals()

Here you can find the source of countDecimals()

Method Source Code

Number.prototype.countDecimals = function () {
    if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
    return this.toString().split(".")[1].length || 0; 
}

Related

  1. timesRepeat(f)
    Number.prototype.timesRepeat = function(f){
      for(var i=0; i<this; i++){
        f();
    
  2. timesTwo(x)
    var x = new Number(2);
    console.log(x);
    Number.prototype.timesTwo = function(x){
      return x * 2;
    };
    console.log(x.timesTwo(x));
    console.log(Number.isNaN(x));
    Number.isInteger = Number.isInteger || function(x){
      return typeof x === "number" &&
    ...
    
  3. clone(handler)
    Number.prototype.clone = function(handler) {
      return new this.constructor(this);
    };
    
  4. concat(value)
    Number.prototype.concat = Number.prototype.concat || function (value) {
      let number = this;
      let digits = exports.digitLength(value);
      number *= Math.pow(10, digits);
      return number + value;
    };
    
  5. cos()
    Number.prototype.cos = function() {
        return Math.cos(this);
    
  6. decimal(nb)
    Number.prototype.to_s       = function(){ return toString(this)     }
    Number.prototype.to_i       = function(){ return parseInt(this, 10) }
    Number.prototype.is_string  = function(){return false}
    Number.prototype.is_hash    = function(){return false}
    Number.prototype.is_array   = function(){return false}
    Number.prototype.is_number  = function(){return true}
    Number.prototype.decimal = function(nb) {
        if( this.toString().indexOf('.') === false ) return this ;
        m = Math.pow(10, nb) ;
    ...
    
  7. decimalPlaces(digits)
    Number.prototype.decimalPlaces = function(digits) {
        var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
            m = this.toString().match(re);
        return m ? parseFloat(m[1]) : this.valueOf();
    };
    
  8. decorateFloat(decimals, decSeparator)
    Number.prototype.decorateFloat = function (decimals, decSeparator) {
      if(decSeparator == undefined)
        decSeparator = '.';
      var price = this;
      var decPattern = '';
      for(var i = 0; i < decimals; i++)
        decPattern += '0';
      price = price.roundFloat(decimals);
      var split = price.toString().split('.');
    ...
    
  9. digits()
    Number.prototype.digits = function() {
        return (Math.floor(this)+"").replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    };