blog

Home / DeveloperSection / Blogs / Currency Format in JavaScript

Currency Format in JavaScript

AVADHESH PATEL3854 01-Aug-2012

Make function for return currency format

<script type="text/javascript">
 function CurrencyFormat(amount) {
     amount = amount.toString().replace(/\$|\,/g, '');
        if (isNaN(amount)) {
         amount = "0";
}
     sign = (amount == (amount = Math.abs(amount)));
     amount = Math.floor(amount * 100 + 0.50000000001);
     cents = amount % 100;
     amount = Math.floor(amount / 100).toString();
     if (cents < 10) {
         cents = "0" + cents;
     }
     for (var i = 0; i < Math.floor((amount.length - (1 + i)) / 3); i++)
     {
       amount = amount.substring(0, amount.length - (4 * i + 3)) + ',' + amount.substring(amount.length - (4 * i + 3));
     }
     return (((sign) ? '' : '-') + amount + '.' + cents);
  }
</script>

Call the above javascript function ‘CurrencyFormat’ on textbox event ‘onblur’.

onblur="this.value=CurrencyFormat(this.value);


Updated 18-Sep-2014
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By