NaN e 0

Marco de Giusti

Nuovo Utente
17 Nov 2015
7
0
0
Buonasera!
Avrei bisogno di sostituire il "not a number" con il valore 0 (zero) in modo che alla compilazione della tabella lo zero venga contato come numero, nel caso ci siano dei dati volutamente mancanti, e quindi utilizzato nel calcolo. Al momento non ho risultato poiché l'operazione prevede l'utilizzo di tutti i dati che se non inseriti si presentano come NaN e quindi non come numeri. Come potrei fare? Vi mostro la parte del codice se può essere d'aiuto.

Codice:
var eeisus = 1;
var eetrue = "TRUE";
var eefalse = "FALSE";
var eedec = ".";
var eeth = ",";
var eedecreg = new RegExp("\\.", "g");
var eethreg = new RegExp(",", "g");
var eecurrencyreg = new RegExp("[$]", "g");
var eepercentreg = new RegExp("%", "g");


function myIsNaN(x) {
    return (isNaN(x) || (typeof x == 'number' && !isFinite(x)));
};


function round(n, nd) {
    if (isFinite(n) && isFinite(nd)) {
        var sign_n = (n < 0) ? -1 : 1;
        var abs_n = Math.abs(n);
        var factor = Math.pow(10, nd);
        return sign_n * Math.round(abs_n * factor) / factor;
    } else {
        return NaN;
    }
};


function sum2(arr, rt, rb) {
    var sum = 0;
    for (var ii = rt; ii <= rb; ii++) {
        sum += arr[ii]
    };
    return sum
};


function sum3(arr, rt, ct, rb, cb) {
    var sum = 0;
    for (var ii = rt; ii <= rb; ii++) {
        for (var jj = ct; jj <= cb; jj++) {
            sum += arr[ii][jj]
        }
    };
    return sum
};


function sumgeneral(cnt, vsum, vcnt, x) {
    var sum = vsum;
    for (var ii = 0; ii < x.length; ii++) {
        sum += sum3(x[ii][0], x[ii][1], x[ii][2], x[ii][3], x[ii][4]);
    };
    return sum;
};


function eeparseFloat(str) {
    str = String(str).replace(eedecreg, ".");
    var res = parseFloat(str);
    if (isNaN(res)) {
        return 0;
    } else {
        return res;
    }
};
var near0RegExp = new RegExp("[.](.*0000000|.*9999999)");


function eedisplayFloat(x) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var str = String(x);
        if (near0RegExp.test(str)) {
            x = round(x, 8);
            str = String(x);
        }
        return str.replace(/\./g, eedec);
    }
};


function eedisplayFloatND(x, nd) {
    if (myIsNaN(x)) {
        return Number.NaN;
    } else {
        var res = round(x, nd);
        if (nd > 0) {
            var str = String(res);
            if (str.indexOf('e') != -1) return str;
            if (str.indexOf('E') != -1) return str;
            var parts = str.split('.');
            if (parts.length < 2) {
                var decimals = ('00000000000000').substring(0, nd);
                return (parts[0]).toString() + eedec + decimals;
            } else {
                var decimals = ((parts[1]).toString() + '00000000000000').substring(
                    0, nd);
                return (parts[0]).toString() + eedec + decimals;
            }
        } else {
            return res;
        }
    }
};

Grazie mille anticipatamente!! :)