Custom Number Formatting method - Javascript Number Operation

Javascript examples for Number Operation:Number Format

Description

Custom Number Formatting method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <script type="text/javascript">
Number.prototype.toNDigits = function (n) {
    return (Math.abs(this) < 1) ?/*  w  w w . j  av  a2s.  c  o  m*/
        this.toFixed(n - 1) :
        this.toPrecision(n);
};
console.log( (100).toNDigits(5) );
console.log( (100.45485485).toNDigits(5) );
console.log( (0.00034700).toNDigits(5) );

      </script>  
   </body>
</html>

Related Tutorials