How to disappear with NaN when I delete field i8

-3

I have a form with two fields i8 and i9 and a calcular function that places the results in a result field. When the i8 field is blank and the calcular() function is called, NaN is written to it. How do I resolve this?

Here's my code:

<script>
String.prototype.formatMoney = function() {
    var v = this;

    if(v.indexOf('.') === -1) {
        v = v.replace(/([\d]+)/, "$1,00");
    }

    v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
    v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");
    v = v.replace(/([\d]+)([\d]{3}),([\d]{2})$/, "$1.$2,$3");

    return v;
};
function id( el ){
    return document.getElementById( el );
}
function getMoney( el ){
//var money = id( el ).value ? id( el ).value.replace( ',', '.' ) : 0; valor sem o R$NaN
var money = id( el ).value.replace( ',', '.' );
return parseFloat( money )*1000;
}
function calcular()
{
    var total = getMoney('i9')-getMoney('i8');
    //id('result').value = 'R$'+ String(total/1000).formatMoney();
    id('result').value = String(total/1000).formatMoney();
}

</script>

<input id="result">
    
asked by anonymous 15.09.2017 / 21:50

1 answer

2

The trick is to use the / p>

In addition, in the isNaN function, you multiply by 1000 and then divide by 1000 in getMoney(el) . This seems to me to be a totally unnecessary gambiarra, and that's why I took it.

Here is your resulting code. To test, click the first blue button Run below:

function formatMoney(value) {
    var negativo = value < 0;
    var v = Math.floor(Math.abs(value) * 100);
    var r = String(v);
    var s = r.length;
    r = s === 1 ? "0,0" + r
            : s === 2 ? "0," + r
            : r.substring(0, s - 2) + "," + r.substring(s - 2, s);
    for (var i = s - 5; i > 0; i -= 3) {
        r = r.substring(0, i) + "." + r.substring(i, r.length);
    }
    return (negativo ? "-" : "") + r;
};

function id(el) {
    return document.getElementById(el);
}

function getMoney(el) {
    var money = parseFloat(id(el).value.replace(',', '.'));
    return isNaN(money) ? 0.0 : money;
}

function calcular() {
    var total = getMoney('i9') - getMoney('i8');
    id('result').value = formatMoney(total);
}
<p>i8: <input type="text" id="i8" /></p>
<p>i9: <input type="text" id="i9" /></p>
<p>Resultado: <input type="text" id="result" readonly /></p>
<input type="button" onclick="javascipt:calcular();" value="Calcular"/>

You may have noticed that I completely changed its calcular() function. That even ceased to be a mix-in method of formatMoney and became a normal function. The reason for this is that your String method did not work to properly format numbers greater than 1 million or with many decimal places.

Here's a test with your original formatMoney method, note the output produced the problems (click the blue Run button below, the second of this answer):

String.prototype.formatMoney = function() {
    var v = this;

    if(v.indexOf('.') === -1) {
        v = v.replace(/([\d]+)/, "$1,00");
    }

    v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
    v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");
    v = v.replace(/([\d]+)([\d]{3}),([\d]{2})$/, "$1.$2,$3");

    return v;
};

document.write("0".formatMoney() + "<br>");
document.write("5".formatMoney() + "<br>");
document.write("10".formatMoney() + "<br>");
document.write("987.65".formatMoney() + "<br>");
document.write("987.6543".formatMoney() + "<br>");
document.write("1000".formatMoney() + "<br>");
document.write("1000.23".formatMoney() + "<br>");
document.write("999999.99".formatMoney() + "<br>");
document.write("1999999.99".formatMoney() + "<br>");
document.write("999999999.99".formatMoney() + "<br>");
document.write("-999999999.99".formatMoney() + "<br>");
document.write("0.05".formatMoney() + "<br>");
document.write("-0.05".formatMoney() + "<br>");
document.write("0.005".formatMoney() + "<br>");

Here's a test of the new version of formatMoney (click the blue Run button below, third and last):

function formatMoney(value) {
    var negativo = value < 0;
    var v = Math.floor(Math.abs(value) * 100);
    var r = String(v);
    var s = r.length;
    r = s === 1 ? "0,0" + r
            : s === 2 ? "0," + r
            : r.substring(0, s - 2) + "," + r.substring(s - 2, s);
    for (var i = s - 5; i > 0; i -= 3) {
        r = r.substring(0, i) + "." + r.substring(i, r.length);
    }
    return (negativo ? "-" : "") + r;
};

document.write(formatMoney(0) + "<br>");
document.write(formatMoney(5) + "<br>");
document.write(formatMoney(10) + "<br>");
document.write(formatMoney(987.65) + "<br>");
document.write(formatMoney(987.6543) + "<br>");
document.write(formatMoney(1000) + "<br>");
document.write(formatMoney(1000.23) + "<br>");
document.write(formatMoney(999999.99) + "<br>");
document.write(formatMoney(1999999.99) + "<br>");
document.write(formatMoney(999999999.99) + "<br>");
document.write(formatMoney(-999999999.99) + "<br>");
document.write(formatMoney(0.05) + "<br>");
document.write(formatMoney(-0.05) + "<br>");
document.write(formatMoney(0.005) + "<br>");
    
16.09.2017 / 00:29