2

I'm doing this:

var refundAmount = parseFloat($('#refundAmount2').val().replace('$',''));
var refundReceived = $('#refundReceived');
var remainderAmount = refundAmount-parseFloat(refundReceived.val().replace('$',''));

alert(parseInt(remainderAmount).toFixed(2));

No matter what I do, the result always ends with 2 decimal places being '.00'. So if the first number is 200.12 and the second is 100.08, it should be alerting me with 100.04 but instead I get 100.00.

Why might this be happening?

halfer
  • 19,824
  • 17
  • 99
  • 186
Damien
  • 4,093
  • 9
  • 39
  • 52
  • 2
    `parseInt(remainderAmount)` converts the number to an integer and then `toFixed(2)` appends `.00` to it. It might be undesired but it's the predictable result. I think you want `alert(parseFloat(remainderAmount.toFixed(2)));` – Ejaz Apr 17 '13 at 15:39

2 Answers2

7

You used parseInt to convert that number to an integer and then used toFixed(2) to convert it to a number with 2 decimal places. Adding 2 decimal places to an integer will always result in .00.

Try

alert(remainderAmount.toFixed(2));

See DEMO.

Antony
  • 14,900
  • 10
  • 46
  • 74
1

You're getting it as an int with parseInt(), then doing the toFixed(). So you're putting decimal places on an int.

djs
  • 220
  • 1
  • 5