954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying decimals in JavaScript??

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.

jobojo
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

hello see this code:
it will print 200 as 200.00

<script language="javascript">
function CurrencyFormatted(amount)
{
      var i = parseFloat(amount);
      if(isNaN(i)) { i = 0.00; }
      var minus = '';
      if(i < 0) { minus = '-'; }
      i = Math.abs(i);
      i = parseInt((i + .005) * 100);
      i = i / 100;
      s = new String(i);
      if(s.indexOf('.') < 0) { s += '.00'; }
      if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
      s = minus + s;
    // alert(s);
    return s;
}
</script>
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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
jobojo
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

You are Welcome!!!!!!

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You