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.

Recommended Answers

All 3 Replies

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;

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

You are Welcome!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.