Because toFixed()
returns a string, you're comparing strings instead of numbers. In a string-based comparison, 99
will come after 3930.85
, and the greater-than-or-equal comparison will evaluate to true.
Getting rid of the call to toFixed()
will solve it.
received = parseFloat($('input[name=amount_received]').val());
total_amount = parseFloat($('input[name=total_bill]').val());
if (received >= total_amount) {
console.log("full");
} else {
console.log("partial");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="amount_received" value="99" />
<input name="total_bill" value="3930.85" />
As a side note, using toFixed()
is unreliable when it comes to rounding. In both current Chrome and Firefox, calling toFixed()
yields the following inconsistent results:
35.655.toFixed(2) // Yields "36.66" (correct)
35.855.toFixed(2) // Yields "35.85" (wrong, should be "35.86")
See also the discussion here and refer to MDN for a reliable rounding method.