Round string and display dollar format with no decimals - Javascript Number Operation

Javascript examples for Number Operation:Number Format

Description

Round string and display dollar format with no decimals

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w  ww . j av  a2  s  .  c  om
function formatNumber(val){
   val = Math.round(parseFloat(val));
   while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
   }
   return '$ ' + val;
}
console.log(formatNumber(1234567.89));
console.log(formatNumber(1.89));
    }

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

Related Tutorials