Nodejs Number Calculate decorateFloat(decimals, decSeparator)

Here you can find the source of decorateFloat(decimals, decSeparator)

Method Source Code

Number.prototype.decorateFloat = function (decimals, decSeparator) {
   if(decSeparator == undefined)
      decSeparator = '.';
   var price = this;
   var decPattern = '';
   /*ww w .j  a v  a2 s.  c  om*/
   for(var i = 0; i < decimals; i++)
      decPattern += '0';
      
   price = price.roundFloat(decimals);
   var split = price.toString().split('.');
   if(split[1] == undefined)
      split[1] = '';
   var decor = decPattern.substring(split[1].length, decimals);
   split[1] = split[1]+decor;
   
   return split.join(decSeparator);
};

Related

  1. 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;
    };
    
  2. cos()
    Number.prototype.cos = function() {
        return Math.cos(this);
    
  3. countDecimals()
    Number.prototype.countDecimals = function () {
        if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
        return this.toString().split(".")[1].length || 0; 
    
  4. 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) ;
    ...
    
  5. 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();
    };
    
  6. digits()
    Number.prototype.digits = function() {
        return (Math.floor(this)+"").replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    };
    
  7. discountPercentage(val)
    Number.prototype.discountPercentage = function(val){
      return this - this.percentage(val);
    };
    
  8. div10()
    Number.prototype.div10 = function () {
      return this/10;
    Object.prototype.say = function (s) {
     console.log('I say ' + s);
    var o = {};
    o.say("Hello");
    var n = 120;
    ...
    
  9. double()
    Number.prototype.double = function() {
      return this * 2;
    };