0

no matter what i try i can't get rid of "NaN" when the data isn't present

Here is function

function decimalFormat(thisNum, places) {
    if (places === -1) places = precision;
    if (parseFloat(thisNum) === "NaN") thisNum = 0;
    if (thisNum === -0.0001) {
        thisNum = "-.---";
    } else {
        thisNum = parseInt(thisNum * Math.pow(10, places)) / (Math.pow(10, places));
        if (thisNum === parseInt(thisNum)) {
            if (places > 0) thisNum += ".";
        }
        for (var x = 0; x < places; x++) {
            if ((parseFloat(thisNum) * (Math.pow(10, x))) === parseInt(thisNum * (Math.pow(10, x)))) thisNum += "0";
            // I get "Nan" in this loop
            // I placed efforts below here
        }
    }
    return thisNum;
}

I have marked where I'm getting "NaN" in the function. I have tried all the following and still gets "Nan" , any advice ? I placed them inside the loop where i have it marked in function

if (thisNum === 'undefined') thisNum = 0;
if (parseFloat(thisNum) === 'undefined') thisNum = 0;
if (parseFloat(thisNum) === "NaN") thisNum = 0;
if (thisNum === "NaN") thisNum = 0;
MShack
  • 642
  • 1
  • 14
  • 33
  • 2
    To check for NaN you use the special function. See https://stackoverflow.com/questions/2652319/how-do-you-check-that-a-number-is-nan-in-javascript – code Aug 12 '22 at 00:52
  • 1
    The only way to check for whether something is not a number is to use the [isNan()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) function. – Mike 'Pomax' Kamermans Aug 12 '22 at 00:53
  • yes , that did the trick , thanks – MShack Aug 12 '22 at 01:02

0 Answers0