简体   繁体   中英

Count with input value 0.00

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);
  };

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM