943,519 Members | Top Members by Rank

Ad:
Oct 8th, 2008
0

Displaying decimals in JavaScript??

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Oct 8th, 2008
0

Re: Displaying decimals in JavaScript??

hello see this code:
it will print 200 as 200.00
javascript Syntax (Toggle Plain Text)
  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>
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 8th, 2008
0

Re: Displaying decimals in JavaScript??

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.

hello see this code:
it will print 200 as 200.00
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Oct 8th, 2008
0

Re: Displaying decimals in JavaScript??

You are Welcome!!!!!!
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Iframe replacement
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: how to maximize table cell height





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC