Nodejs Number Calculate float()

Here you can find the source of float()

Method Source Code

var num = 5;//w w  w.  j ava 2s  . c  om
Number.prototype.float = function(){ 
         return this.toFixed(2);
}


Number.prototype.int = function(){ 
         return parseInt(this) ;
}

console.log(num.float());
console.log(num);
var num2 = 90.897;

console.log(num2.int());
console.log(num2.float());
console.log(num2);

Related

  1. fe(x)
    Number.prototype.fe = function(x)
      var s = x.toString();
      var point = s.search(/\./);
      if (point == -1)
        return (Math.abs(this - x) < 0.5);    
      else
    ...
    
  2. fillZero(len)
    Number.prototype.fillZero = function (len)
      var s = this.toString(10);
      while (s.length<len) s = "0" + s;
      return s;
    
  3. firstDigit()
    Number.prototype.firstDigit = Number.prototype.firstDigit || function () {
      let number = this;
      while (number >= 10) {
        number = Math.floor(number / 10);
      return number;
    };
    
  4. fix(n)
    Number.prototype.fix = function (n) {
        var val = String(this);
        n = Math.abs(n);
        val = val.split('.');
        val[1] = val[1] || '';
        var m = val[1].length;
        if (m < n) {
            m = n - m;
            while (m > 0) {
    ...
    
  5. fizz()
    Number.prototype.fizz = function() {
        return this % 3 === 0 ? "fizz" : "";
    };
    Number.prototype.buzz = function() {
        return this % 5 === 0 ? "buzz" : "";
    };
    for (var i = 1; i <= 15; i++) {
        var candidate = Number(i).fizz() + Number(i).buzz();
        console.log(candidate ? candidate : i);
    ...
    
  6. floatMethod()
    Number.prototype.floatMethod = function() {
        return parseFloat(this).toFixed(2);
    };
    function convert_float() {
        var n = 60.00;
       console.log(n.floatMethod());
    convert_float(n);
    
  7. frac()
    Number.prototype.frac = function() {
      return this - Math.floor(this);
    };
    
  8. fuzzyEqual(otherNumber)
    Number.prototype.fuzzyEqual = function(otherNumber)
        return Math.abs(this - otherNumber) <= 0.00001;
    
  9. gcd(b)
    Number.prototype.gcd = function(b) {
      return (b == 0) ? this : this.gcd(this % b);