Nodejs Number Convert toPrecisionFixed(precision)

Here you can find the source of toPrecisionFixed(precision)

Method Source Code

Number.prototype.toPrecisionFixed = function(precision) {
   // www.  j a  v a 2s .c o  m
   // use standard toPrecision method
   var n = this.toPrecision(precision);
   
   // ... but replace +ve exponential format with trailing zeros
   n = n.replace(/(.+)e\+(.+)/, function(n, sig, exp) {
     sig = sig.replace(/\./, '');       // remove decimal from significand
     l = sig.length - 1;
     while (exp-- > l) sig = sig + '0'; // append zeros from exponent
     return sig;
   });
   
   // ... and replace -ve exponential format with leading zeros
   n = n.replace(/(.+)e-(.+)/, function(n, sig, exp) {
     sig = sig.replace(/\./, '');       // remove decimal from significand
     while (exp-- > 1) sig = '0' + sig; // prepend zeros from exponent
     return '0.' + sig;
   });
   
   return n;
}

// Merge Objects

Object.merge = function(o1, o2) {
   for(var i in o2) { o1[i] = o2[i]; }
   return o1;
}

Related

  1. toCounter()
    Number.prototype.toCounter = function () {
        var d = this;
        d = Number(d);
        var h = Math.floor(d / 1000 / 3600 );
        var m = Math.floor(d / 1000 % 3600 / 60);
        var s = Math.floor(d / 1000 % 3600 % 60);
        var ms = (d % 3600000 % 60000 % 1000);
        var tenths = Math.floor(ms / 100);
        return ((h > 0 ? h + ":" + (m < 10 ? "0" : "") : "") + m + ":" + (s < 10 ? "0" : "") + s + "." + tenths);
    ...
    
  2. toDigits()
    Number.prototype.toDigits = Number.prototype.toDigits || function () {
      let number = this;
      let digits = [];
      while (number > 0) {
        digits[digits.length] = number % 10;
        number = Math.floor(number / 10);
      return digits.reverse();
    };
    ...
    
  3. toDollars(c, printSign)
    Number.prototype.toDollars = function(c, printSign){
      var n = this,
        c = isNaN(c = Math.abs(c)) ? 2 : c,
        d = ".",
        t = ",",
        s = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0,
        sign = printSign ? '$': '';
    ...
    
  4. toMinutes()
    Number.prototype.toMinutes = function(){
      return (Math.floor(parseInt(this)/60)).addTrailingZero()+":"+(parseInt(this)%60).addTrailingZero();
    };
    
  5. toNumber( val )
    var NumberUtils = new function() {
        this.toNumber = function( val ) {
            if(val==null || val=="") return null;
        val = val.replace(',', '');
            return eval(val);
        this.toInteger = function( val ) {
        val = val.replace(',', '');
            return parseInt(val, 10); 
    ...
    
  6. toPrecision(length)
    Number.prototype.toPrecision = function toPrecision(length) {
        return parseFloat(this.toFixed(length));
    };
    
  7. toS()
    function numberToPx(number) {
      return number + "px"; 
    function getMilliseconds(timestampStr) {
      var timestampStrSplit = timestampStr.split(".");
      var milli = Date.parse("January 1, 1970 " + timestampStrSplit[0] + " UTC");
      if (timestampStrSplit.length == 1)
        return milli;
      else (timestampStrSplit.length == 2)
    ...
    
  8. to_feet()
    Number.prototype.to_feet = function() {
      return this / 0.3048;
    };