javascript calculations

Results 1 to 2 of 2

Thread: javascript calculations

  1. #1
    Join Date
    Dec 1969

    Default javascript calculations

    Ok this is probably very simple but I&#039;m having a nightmare with it and probably the reason I hate javascript.<BR>Ok I have a text box where user inputs a monatary amount and the format of that value has to be (10,000.00) after that the user also inputes an interest rate like 15%and the format of that will be (15) now all I have to do is calculate the yearly interest like 10,000.00 * 0.15 .I&#039;ve tried to use parseInt & parseFloat but can&#039;t get the right values.<BR>How can I acomplish this<BR>here is some of my code<BR><BR>function calculate(){<BR><BR>var frm = document.investdata;<BR>var invamnt = parseFloat(frm.reqAmount.value);<BR>var intrate = parseFloat(frm.reqInterestRate.value);<BR>var intamnt = invamnt * intrate;<BR>var ttlamnt = invamnt + intamnt ;<BR><BR>//alert("amountis"+invamnt);<BR>//alert("rate is"+intrate);<BR>//alert("interest is"+intamnt );<BR>//alert("total is"+ttlmat);<BR><BR><BR><BR>}<BR>thankyou

  2. #2
    Join Date
    Dec 1969

    Default Can't calculate with non-numbers!

    A value such as <BR> 10,000.00<BR>is *NOT* a valid number for conversion via parseFloat()!<BR><BR>(Commas are not allowed.)<BR><BR>So you are hosed from the start.<BR><BR>You need to get rid of extraneous characters.<BR><BR>One way:<BR> var re = /[^0-9-.]/g<BR> var invamnt = parseFloat( frm.reqAmount.value.replace(re,"") );<BR><BR>That replace will strip out all characters except the digits, the minus sign, and the period. You could still get an illegal number (one with two minus signs?), but it&#039;s a start.<BR><BR>But then you need to CHECK to be sure you *DID* get a valid number!<BR> if ( isNaN( invamnt ) )<BR> {<BR> alert("Invoice amount is NOT a legal number!");<BR> ...<BR> }<BR><BR>Finally, if the user enters 15 for the interest rate and you simply multiply by that...well, you need to go back to 6th grade arithmetic!<BR><BR>To multiply by a PERCENTAGE, you have to first divide the percentage by 100!<BR> total = amount * ( percentage / 100.0 );<BR><BR>Hokay?<BR><BR>

Posting Permissions

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