Posted By

sekihin on 03/10/09


Tagged

javascript format


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jamesming
tux-world


Convert Number To Currency


 / Published in: JavaScript
 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> moneyConvert Test Document </TITLE>
  5. <script type="text/javascript">
  6. <!--
  7. function moneyConvert(value){
  8. var buf = "";
  9. var sBuf = "";
  10. var j = 0;
  11. value = String(value);
  12.  
  13. if (value.indexOf(".") > 0) {
  14. buf = value.substring(0, value.indexOf("."));
  15. } else {
  16. buf = value;
  17. }
  18. if (buf.length%3!=0&&(buf.length/3-1) > 0) {
  19. sBuf = buf.substring(0, buf.length%3) + ",";
  20. buf = buf.substring(buf.length%3);
  21. }
  22. j = buf.length;
  23. for (var i = 0; i <(j/3-1); i++) {
  24. sBuf = sBuf+buf.substring(0, 3) + ",";
  25. buf = buf.substring(3);
  26. }
  27. sBuf = sBuf+buf;
  28. if (value.indexOf(".") > 0) {
  29. value = sBuf + value.substring(value.indexOf("."));}
  30. else {
  31. value = sBuf;
  32. }
  33. return value;
  34. }
  35.  
  36. function btn_click() {
  37. value=document.form1.text1.value;
  38. document.form1.text2.value=moneyConvert(value);
  39. }
  40. / -->
  41. </script>
  42. </HEAD>
  43.  
  44. <BODY>
  45. <form method="post" action="" name="form1">
  46. <input type="text" name="text1" value="123456.789"><br>
  47. <input type="text" name="text2"><br>
  48. <input type="button" name="btn" onclick="btn_click()" value="button">
  49. </form>
  50. </BODY>
  51. </HTML>

Report this snippet  

You need to login to post a comment.