Nodejs Number Add add(arg)

Here you can find the source of add(arg)

Method Source Code

Number.prototype.add = function(arg){
   var r1, r2, m;
   try{r1 = this.toString().split(".")[1].length}catch(e){r1=0}
   try{r2 = arg.toString().split(".")[1].length}catch(e){r2=0}
   m = Math.pow(10, Math.max(r1, r2))
   return (this*m+arg*m)/m
}

// ?// ww w . ja va 2  s. co m
Number.prototype.sub = function(arg){
   return this.add(-arg)
}

// ?
Number.prototype.mul = function(arg){
   var m = 0, sl = this.toString(), s2 = arg.toString();
   try{m += sl.split(".")[1].length}catch(e){}
   try{m += s2.split(".")[1].length}catch(e){}
   return m = Number(sl.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

// ?
Number.prototype.div = function(arg){
   var t1 = 0, t2 = 0, r1, r2;
   try{t1 = this.toString().split(".")[1].length}catch(e){}
   try{t2 = arg.toString().split(".")[1].length}catch(e){}
   with(Math){
      r1 = Number(this.toString().replace(".", ""))
      r2 = Number(arg.toString().replace(".", ""))
      return (r1/r2)*pow(10, t2-t1)
   }
}

Number.prototype.noRound = function(digit){
   var temp = this.toFixed(digit+1);
   return temp.substring(0,temp.lastIndexOf('.')+(digit+1))
}

Related

  1. add()
    Number.prototype.add = function () {
        return this + [].slice.apply(arguments).reduce(function (a, b) {
                "use strict";
                return a + b;
            },0);
    
  2. add(a)
    Number.prototype.add = function(a) {
      return this.valueOf() + a;
    };
    Number.prototype.subtract = function(a) {
      return this.valueOf() - a;
    };
    Number.prototype.multiply = function(a) {
      return this.valueOf() * a;
    };
    ...
    
  3. add(n)
    Number.prototype.add = function(n){
      return this+n
    Number.prototype.subtract = function(n){
      return this-n
    Number.prototype.multiply = function(n){
      return this*n
    Number.prototype.divide = function(n){
      return this/n
    Number.prototype.square = function(n){
      var _ = this;
      return Math.pow(_,2)
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  4. add(n) return this+n }
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  5. add(num)
    Number.prototype.add = function(num) {
      return this + num;
    var n = 5;
    assert(n.add(3) == 8, 'It works when the number is in a variable.');
    assert((5).add(3) == 8, 'Also works if a number is wrapped in parentheses.');
    assert(5.add(3) == 8, 'What about a simple literal?');
    
  6. add(num)
    Number.prototype.add = function(num)
        return this+num;
    };
    describe("6.13", function () {
        it("It works when the number is in a variable.", function () {
             expect(n.add(3)).toEqual(8);
        });
        it("It works when the number is in a parentheses.", function () {
    ...