Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

JavaScript Programming

JavaScript > Code Examples

Currency Format

Currency Format <html> <head> <title>Happy Codings :-) JavaScript - Learn Web Development</title> </head> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function formatCurrency(num) { num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + '$' + num + '.' + cents); } // End --> </script> </head> <body> <center> <form name=currencyform> Enter a number then click the button: <input type=text name=input size=10 value="1000434.23"> <input type=button value="Convert" onclick="this.form.input.value=formatCurrency(this.form.input.value);"> <br><br> or enter a number and click another field: <input type=text name=input2 size=10 value="1000434.23" onBlur="this.value=formatCurrency(this.value);"> </form> </center> </body> </html>