Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Issue in Displaying NaN values

    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:
    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Despite the similar sounding names, Java is not the same as Javascript.
    Moving from Java forum to Javascript forum.

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    You could test for NaN

    Code:
    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:
    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);
        if (!isNaN(P28_max_convert)) {
            var P28_maxdoz = document.getElementById("P28_maxdoz").value = P28_max_convert;
        }
    }
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • Users who have thanked blaze4218 for this post:

    newphpcoder (03-08-2012)

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Or you can convert NaN values to zeroes and also use Number() to convert empty values to zeroes.

    Code:
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •