SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $NaN.000000 Problem

    hi there i'm having a problem with my javascript i'm getting $NaN.000000 for my textfields. i'm only getting a correct number for valTotalGA.
    for valRateGA, valRateCA, valTotalCA, valRate,valTotal i'm getting $NaN.000000

    please if any1 can look at my code and help me with it.
    cheers guys

    Code:
    calcTotals: function(){
       	var valTier = Format.unformatDecimalCore(Util.getSelValue(this.tier));
       	var valLCI = Format.unformatDecimalCore(document.getElementById(this.lci).value);
       	var valConst = Format.unformatDecimalCore(document.getElementById(this.GAConst).value);
       	var valGUseSF = Format.unformatDecimalCore(document.getElementById(this.GUseSF).value);
       	var valWHSF = Format.unformatDecimalCore(document.getElementById(this.WHSF).value);
       	var valUniqueSF = Format.unformatDecimalCore(document.getElementById(this.UniqueSF).value);
       	
        
        if(Util.isZero(valTier)) {valTier = 0;}
       	if(Util.isZero(valConst)) {valConst = 0;}
       	if(Util.isZero(valGUseSF)) {valGUseSF = 0;}  	
       	if(Util.isZero(valUniqueSF)) {valUniqueSF = 0;}
        if(Util.isZero(valLCI)) {valLCI = 0;}
       	
        var valTotalGA = 0;
       	document.getElementById(this.TotalGA).value = '';
        
        if(Util.isZero(valWHSF)) {
        valWHSF = 0;
    		valTotalGA = parseFloat(valConst) * parseFloat(valLCI) * (parseFloat(valGUseSF) + parseFloat(valUniqueSF));
        }
        else	{
        valTotalGA = parseFloat(valConst) * parseFloat(valLCI) * (parseFloat(valGUseSF) + parseFloat(valUniqueSF) + parseFloat(valWHSF * 0.2));
        }      
          document.getElementById(this.TotalGA).value = Format.formatAmountCore(valTotalGA,2);
           
        var valRateGA = 0;
       	document.getElementById(this.RateGA).value = '';  
        valRateGA = parseFloat(valTotalGA) / (parseFloat(valGUseSF) + parseFloat(valWHSF) + parseFloat(valUniqueSF));	
        document.getElementById(this.RateGA).value = Format.formatAmountCore(valRateGA,6);
    
       	var valRateCA = 0;
       	document.getElementById(this.RateCA).value = '';
    		valRateCA = parseFloat(valRateGA) * parseFloat(valTier);
        document.getElementById(this.RateCA).value = Format.formatAmountCore(valRateCA,6);
        	
        var valRate = parseFloat(valRateGA) + parseFloat(valRateCA);
        document.getElementById(this.Rate).value = Format.formatAmountCore(valRate,6);
       
       	var valTotalCA = 0;
       	document.getElementById(this.TotalCA).value = '';
    		valTotalCA = parseFloat(valRateCA) * (parseFloat(valGUseSF) + parseFloat(valUniqueSF));
        document.getElementById(this.TotalCA).value = Format.formatAmountCore(valTotalCA,2);
    
       	var valTotal = 0;
       	document.getElementById(this.Total).value = '';
    		valTotal = parseFloat(valTotalGA) + parseFloat(valTotalCA);
        document.getElementById(this.Total).value = Format.formatAmountCore(valTotal,2);
    
       },

  2. #2
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The problem is, that Javascript doesn't have different data types, and the parser therefore guesses (and frequently wrong, I might add). The easiest way to get around it, is to force the parser to use the variables as numbers, e.g.
    Code:
    var firstVariable = document.forms["form"].elements["elementI"].value;
    var secondVariable = document.forms["form"].elements["elementII"].value;
    var variableSum = ((firstVariable / 2 * 2) + (secondVariable / 2 * 2));
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so where and how do i fix my code? can u help please.
    Last edited by dausboy; Jul 28, 2006 at 07:55.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any other suggestions please?????

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please help any1????

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    NaN

    To make a numeric input string a number try Math.abs eg
    HTML Code:
    var firstVariable = Math.abs(document.forms["form"].elements["elementI"].value);

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how can i apply that in my code please? can you point it out for me coz i have tried everything and it still doesn't work

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    Math.abs

    Quote Originally Posted by dausboy
    how can i apply that in my code
    Just like I showed. Put any input that you want to make sure is treated as a number instead of a string inside Math.abs().

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's the jsp code for that js part. i have tried what u guys have told me, it didn't work. i'm still getting $NaN.000000.

    Code:
    <tr>
                 <td><input:emptySelect name="STATE_CODE" style="width:45;" label="State" load="AllowanceCalc.setState(this.elId);AllowanceCalc.buildState();" onChange="Util.enableElement(Util.getAnotherElement(this.id,1));" clean="Util.enableElement(this.elId)"/></td>
                 <td><input:button onClick="AllowanceCalc.updCity();Util.disableElement(this.id);" class="imageButton" value="<img src='images/protected/refresh_btn.gif' title='update'>" disabled="true"/></td>
                </tr>
             </table>
    			</td>
    		  </tr>
    		  <tr>
    			<td colspan=6>&nbsp;</td>
    		  </tr>
    		  <tr>
    			<td colspan=4 style="text-align:center"><input:label label="Space (USF)"/></td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    		  </tr>
    		  <tr>
    			<td style="width:108;text-align:center"><input:label label="General Use"/></td>
    			<td style="width:108;text-align:center"><input:label label="Warehouse"/></td>
    			<td style="width:108;text-align:center"><input:label label="Unique"/></td>
    			<td style="width:144;text-align:center"><input:label label="Total"/></td>
    			<td colspan=2>&nbsp;</td>
    		  </tr>
    		  <tr>
    			<td style="text-align:center"><input:decimal data="GENERAL_USE_SQFT" style="width:104;" maxlength="10" decPlaces="2" load="AllowanceCalc.GUseSF=this.elId;" maxVal="9999999.99" minVal="0" onBlur="AllowanceCalc.calcSFTotal();" label="General Use Square Feet"/></td>
    			<td style="text-align:center"><input:decimal data="WAREHOUSE_SQFT" style="width:104;" maxlength="10" decPlaces="2" load="AllowanceCalc.WHSF=this.elId;" maxVal="9999999.99" minVal="0" onBlur="AllowanceCalc.calcSFTotal();" label="Warehouse Square Feet"/></td>
    			<td style="text-align:center"><input:decimal data="UNIQUE_SQFT" style="width:104;" maxlength="10" decPlaces="2" load="AllowanceCalc.UniqueSF=this.elId;" maxVal="9999999.99" minVal="0" onBlur="AllowanceCalc.calcSFTotal();" label="Unique Square Feet"/></td>
    			<td style="text-align:center"><input:decimal data="TOTAL_RSF" style="width:140;" disabled="true" decPlaces="2" load="AllowanceCalc.TotalSF=this.elId;"/></td>
    			<td colspan=2>&nbsp;</td>
    		  </tr>
    		</table>
    	</td>
      </tr>
      <tr>
        <td>
    		<table width="100%" border="0" cellspacing="0" cellpadding="2" class="table_id" id="tbl_2">
    		  <tr>
    			<td colspan="4"><input:label class="subtitle" label="Allowance"/></td>
    		  </tr>
    		  <tr>
    			<td style="width:222;text-align:right"><input:label label="Customization Tier"/></td>
    			<td style="width:108">
    			 <table cellpadding="0" cellspacing="0" border="0">
    			    <tr>
    			       <td><input:select data="FACTOR" style="width:45;" load ="AllowanceCalc.setTier(this.elId);" label="Customization Tier" onChange="Util.enableElement(Util.getAnotherElement(this.id,1));"/></td>
    			       <td><input:button onClick="Util.disableElement(this.id);AllowanceCalc.calcSFTotal();" class="imageButton" value="<img src='images/protected/refresh_btn.gif' title='update'>" disabled="true"/></td>
    			    </tr>
    			 </table>
    			</td>
    			<td style="width:144">&nbsp;</td>
    			<td rowspan=5>&nbsp;</td>
    		  </tr>
    		  <tr>
    			<td style="text-align:right"><input:label label="Local Construction Index"/></td>
    			<td><input:decimal data="SEL.LCI" style="width:45;text-align:right;" disabled="true" decPlaces="2" load="AllowanceCalc.lci=this.elId;"/></td>
    			<td>&nbsp;</td>
    		  </tr>
    		  <tr>
    			<td>&nbsp;</td>
    			<td style="text-align:center"><input:label label="Rate($/USF)"/></td>
    			<td style="text-align:center"><input:label label="Total"/></td>
    		  </tr>
    		  <tr>
    			<td style="text-align:right"><input:label label="General Allowance"/></td>
    			<td><input:amount data="GA_RATE" style="width:104;" disabled="true" decPlaces="6" load="AllowanceCalc.RateGA=this.elId;"/></td>
    			<td><input:amount data="GA_TOTAL" style="width:140;" disabled="true" decPlaces="2" load="AllowanceCalc.TotalGA=this.elId;"/></td>
    		  </tr>
    		  <tr>
    			<td style="text-align:right"><input:label label="Custom Allowance"/></td>
    			<td><input:amount data="CA_RATE" style="width:104;" disabled="true" decPlaces="6" load="AllowanceCalc.RateCA=this.elId;"/></td>
    			<td><input:amount data="CA_TOTAL" style="width:140;" disabled="true" decPlaces="2" load="AllowanceCalc.TotalCA=this.elId;"/></td>
    		  </tr>
    		  <tr>
    			<td style="text-align:right"><input:label label="Total Allowance"/></td>
    			<td><input:amount data="RATE" style="width:104;" disabled="true" decPlaces="6" load="AllowanceCalc.Rate=this.elId;"/></td>
    			<td><input:amount data="TOTAL" style="width:140;" disabled="true" decPlaces="2" load="AllowanceCalc.Total=this.elId;"/></td>
    		  </tr>
    		  <tr>
    			<td colspan=4 style="text-align:right">
    				<%@ include file="calcButtons.jsp" %>
    			</td>
    		  </tr>
    		</table>
    	</td>
      </tr>

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    debug

    It may be time to resort to "alert" trouble-shooting. eg.
    Code:
    var valTier = .....
    alert("valTier: " + valTier);
    var valLCI = .....
    alert("valLCI: " + valLCI);
    var valConst = .....
    alert("valConst: " + r);
    var valGUseSF = .....
    alert("valGUseSF: " + valGUseSF);
    var valWHSF = .....
    alert("valWHSF: " + valWHSF);
    var valUniqueSF = .....
    alert("valUniqueSF: " + valUniqueSF);
    Then you can see where the problem/s is/are.

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok found out that var valGUseSF, var valWHSF, var valUniqueSF are empty. how to fix it then???

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    this.elId

    Unless I'm missing something, I see a lot of (this.elId) in inputs that don't have an id attribute. Are you referencing them as an array somewhere?

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i am using this function as well.
    the problem is that valGUseSF, valWHSF and valUniqueSF are being evaluated before valTotalGA. and valTotalGA is a calculation of these three. its like a deadlock.

    Code:
    calcSFTotal: function(){ 
       	var valGUseSF = Format.unformatDecimalCore(document.getElementById(this.GUseSF).value);
       	var valWHSF = Format.unformatDecimalCore(document.getElementById(this.WHSF).value);
       	var valUniqueSF = Format.unformatDecimalCore(document.getElementById(this.UniqueSF).value);
       	
       	var valTotalSF = 0;
       	document.getElementById(this.TotalSF).value = '';
       	
       	if(Util.isZero(valGUseSF)) {valGUseSF = 0;}
       	if(Util.isZero(valWHSF)) {valWHSF = 0;}
       	if(Util.isZero(valUniqueSF)) {valUniqueSF = 0;}
    
    		valTotalSF = parseFloat(valGUseSF) + parseFloat(valWHSF) + parseFloat(valUniqueSF);
    		valTotalSF = Format.formatDecimalCore(valTotalSF,2);
        document.getElementById(this.TotalSF).value = valTotalSF;
       	this.calcTotals();
       },


Bookmarks

Posting Permissions

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