Formatting Price Input - Javascript Number Operation

Javascript examples for Number Operation:Number Format

Description

Formatting Price Input

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <input type="text" id="price" onchange="formatPrice(this)"> 
      <script type="text/javascript">
function formatPrice(obj) {//from  w  w w. ja  v a  2 s  .  c o m
   //remove any decimal
   p = obj.value.replace('.','');
  //remove the last 2 digits
   var l = p.substring(-2, p.length-2);
  //get the last 2 digits
  var r = p.substring(p.length-2,p.length);
  //update the value
  obj.value = l + '.' + r;
}

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

Related Tutorials