Reputation: 287
I have written a code that will count for me the difference between input value field one and input value field to, but it seems not working when i try to count with the value of 0.00. Can anbody help?
Here is my code
function getMutaties(el) {
el.value = (parseFloat(el.value) || el.value).toFixed(2);
var numVal1 = Number(document.getElementById("start_balance").value);
var numVal2 = Number(document.getElementById("end_balance").value);
var totalValue = numVal2 - numVal1
document.getElementById("mutations").value = totalValue.toFixed(2);
getVerschil();
}
Here are my input fields:
<input type="text" placeholder="Begin saldo" name="start_balance" id="start_balance" onchange="setTwoNumberDecimal(this)" onkeypress="return isNumber(event, this)">
<input type="text" placeholder="Eind saldo" name="end_balance" id="end_balance" onchange="getMutaties(this)" onkeypress="return isNumber(event, this)">
Here is the setTwoNumberDecimal function:
function setTwoNumberDecimal(el) {
el.value = (parseFloat(el.value) || el.value).toFixed(2);
};
Upvotes: 0
Views: 88
Reputation: 3425
setTwoNumberDecimal will throw an error when "0.00" is provided
function setTwoNumberDecimal(el) {
el.value = (parseFloat(el.value) || el.value).toFixed(2);
};
parseFloat("0.00") will be cast to false in (parseFloat(el.value) || el.value)
and el.value will remain string and you cannot call .toFixed(2) on string.
Upvotes: 1