Displaying decimals in JavaScript??

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Displaying decimals in JavaScript??

 
0
  #1
Oct 8th, 2008
I am new to JavaScript and have what may be a very simple and easy question to most. I am writing a simple block of code that I need to display dollar amounts with 2 decimals for change. For example instead of displaying the number 5, I need it to display 5.00. How can I do this? Any help will be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Displaying decimals in JavaScript??

 
0
  #2
Oct 8th, 2008
hello see this code:
it will print 200 as 200.00
  1. <script language="javascript">
  2. function CurrencyFormatted(amount)
  3. {
  4. var i = parseFloat(amount);
  5. if(isNaN(i)) { i = 0.00; }
  6. var minus = '';
  7. if(i < 0) { minus = '-'; }
  8. i = Math.abs(i);
  9. i = parseInt((i + .005) * 100);
  10. i = i / 100;
  11. s = new String(i);
  12. if(s.indexOf('.') < 0) { s += '.00'; }
  13. if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
  14. s = minus + s;
  15. // alert(s);
  16. return s;
  17. }
  18. </script>
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Re: Displaying decimals in JavaScript??

 
0
  #3
Oct 8th, 2008
Good morning Shanti and thank you for your reply. While I did not use the code you provided because many of the characters and items were beyond my understanding, your code still did help me to get the wheels turning in my head and you helped me to solve my problem. Thanks again for your help, it was greatlyAppreciated.

Originally Posted by Shanti Chepuru View Post
hello see this code:
it will print 200 as 200.00
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Displaying decimals in JavaScript??

 
0
  #4
Oct 8th, 2008
You are Welcome!!!!!!
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC