elabuwa Posted March 14, 2010 Share Posted March 14, 2010 Hi guys, did some googling and the only to i came through was big as functions written separately to get a number formatted in the currency method. You the know every three digits seperated with a comma and rounded upto two decimal points. In php it is easy as value = number_format($number,2). I was wondering if there was a similar way in javascript. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/195190-formatting-number-to-currency-with-the-commas-etc/ Share on other sites More sharing options...
slurpee Posted March 15, 2010 Share Posted March 15, 2010 If jQuery is available to you, how about: http://plugins.jquery.com/project/currencyFormat Quote Link to comment https://forums.phpfreaks.com/topic/195190-formatting-number-to-currency-with-the-commas-etc/#findComment-1026224 Share on other sites More sharing options...
Psycho Posted March 15, 2010 Share Posted March 15, 2010 Here's a simple function for you <HTML> <HEAD> <script> function formatCurrency(fieldObj) { if (isNaN(fieldObj.value)) { return false; } fieldObj.value = '$ ' + parseFloat(fieldObj.value).toFixed(2); return true; } </script> </HEAD> <BODY> <input type="text" name="amount" onchange="formatCurrency(this);" /> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/195190-formatting-number-to-currency-with-the-commas-etc/#findComment-1026577 Share on other sites More sharing options...
Ang3l0fDeath Posted May 23, 2010 Share Posted May 23, 2010 I'm Posting Here regarding a way to format numbers like in PHP with number_format. Here is my javascript verison i wrote yesterday. Hope it helps you guys who are looking. This is simply to add commons & mine is only for positive numbers. function number_format(input_){ if(input_>0){var input_string=input_.toString();var rebuild='';var rebuild_remaining=input_string; var loop_='no'; for(i=0; rebuild_remaining.length>3; i++){loop_='yes'; rebuild = rebuild_remaining.substr((rebuild_remaining.length-3),3)+','+rebuild; rebuild_remaining=rebuild_remaining.substr(0,(rebuild_remaining.length-3)); }if(loop_==='yes'){rebuild = rebuild_remaining+','+rebuild; rebuild = rebuild.substr(0,(rebuild.length-1));}else{rebuild=input_string;} return rebuild;}else{return 0;}} Simple way to understand this code, you input a pure number, not a string. the number is turned into a string inside this function so it can count the length of the string, you cant count the length of a number. Secondly i installed a loop that will loop though our number string and slowly subtract 3 end digit numbers at a time and adding a , (comma) after each. The string is being rebuild into a new varible which is later edited with the correct formating and removing an extra comma from the end of the process. So far i havent had 1 problem with this script and it works amazing with my other function that does the var cost = price * amount; var cost = number_format(cost); document.getElementById('div_id').innerHTML=cost; document.getElementById('input_id').value=cost; Again i hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/195190-formatting-number-to-currency-with-the-commas-etc/#findComment-1062171 Share on other sites More sharing options...
ignace Posted May 23, 2010 Share Posted May 23, 2010 Why not just use money_format. For those systems that do not have the function, you can find one in the comments. Quote Link to comment https://forums.phpfreaks.com/topic/195190-formatting-number-to-currency-with-the-commas-etc/#findComment-1062222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.