...

View Full Version : Issue in Displaying NaN values



newphpcoder
03-07-2012, 11:18 AM
Hi..

I created a function and I encountered problem because I got a NaN values. And I want to resolve it..I don't want to display NaN values. instead of NaN I just want it blank.

here is my code:


function doz(oText){
var P27_max = document.getElementById("P27_max").value;
var P27_max_convert = parseFloat(P27_max) + parseFloat(1);
var P27_maxdoz = document.getElementById("P27_maxdoz").value = P27_max_convert;


var P28_max = document.getElementById("P28_max").value;
var P28_max_convert = parseFloat(P28_max) + parseFloat(2);
var P28_maxdoz = document.getElementById("P28_maxdoz").value = P28_max_convert;
}


when I entered data in
P27_max so the P27_maxdoz was computed but the problem is the P28_max has also value 'NaN'.

How can I eliminate or did not display NaN

Thank you

Fou-Lu
03-07-2012, 09:48 PM
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.

blaze4218
03-07-2012, 10:32 PM
You could test for NaN



if(isNaN(P28_max_convert))// this can be misleading in some browsers... it can return false (which means it found a number) if you use a string containing a number. i.e isNaN('2') returns false. just another part of the loosely typed nature of javascript

isnan = function(v){return typeof v!='number'}

if(isnan(P28_max_convert))// this will return true for '2'. which would be important if you needed to know which of the following values was a number, and which was a string: 234 || '567'


either way... you test for nan or test for NaN and not execute if found:


function doz(oText){
var P27_max = document.getElementById("P27_max").value;
var P27_max_convert = parseFloat(P27_max) + parseFloat(1);
var P27_maxdoz = document.getElementById("P27_maxdoz").value = P27_max_convert;


var P28_max = document.getElementById("P28_max").value;
var P28_max_convert = parseFloat(P28_max) + parseFloat(2);
if (!isNaN(P28_max_convert)) {
var P28_maxdoz = document.getElementById("P28_maxdoz").value = P28_max_convert;
}
}

glenngv
03-08-2012, 09:02 PM
Or you can convert NaN values to zeroes and also use Number() to convert empty values to zeroes.


function doz(oText){
var P27_max = Number(document.getElementById("P27_max").value);
var P27_max_convert = (parseFloat(P27_max) + parseFloat(1)) || 0;
var P27_maxdoz = document.getElementById("P27_maxdoz").value = P27_max_convert;


var P28_max = Number(document.getElementById("P28_max").value);
var P28_max_convert = (parseFloat(P28_max) + parseFloat(2)) || 0;
var P28_maxdoz = document.getElementById("P28_maxdoz").value = P28_max_convert;
}
BTW, why are you parseFloat'ing 1 and 2? --> parseFloat(1) and parseFloat(2)
Why not use them directly?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum