Re: Javascript Calculations

by "Cyanide _7" <leo7278(at)hotmail.com>

 Date:  Fri, 14 Jan 2000 19:54:30 CST
 To:  SusanW(at)inscodico.com
 Cc:  hwg-languages(at)hwg.org
  todo: View Thread, Original
try this:

<!-- Begin
function dollarAmount(num) {
  num = num.toString().replace(/\$|\,/g,'');
  cents = "."+Math.floor((num*100)%100);
  num = Math.floor(num).toString();

  if (isNaN(num)){
    alert("This does not appear to be a valid number.  Please try again.");
    return '';
  } else {

  while(cents.length<3)
    cents += "0";

  num = num.toString();
  numOfCommas = Math.floor((num.length-1)/3);
  for (var i = 0; i < numOfCommas; i++)
    num = num.slice(0,-(4*i+3))+','+num.slice(-(4*i+3));

  return ('$' + num + cents);
  }
}

function twoPercent() {
  with(document.ecommerce)
    premium.value = dollarAmount(bondAmt.value.replace(/\$|\,/g,'')*
                                 0.02).slice(1);
}
//-->

and from the form elements:

<input type="text" name="bondAmt" size="14"
       onBlur="this.value = dollarAmount(this.value);">

<input type="button" name="calculate" value="Calculate Premium"
       onClick="twoPercent();">

i used a currency formatting script i wrote a while ago. - Cyanide_7

>From: Susan Walsh <SusanW(at)inscodico.com>
>To: "'hwg-languages(at)hwg.org'" <hwg-languages(at)hwg.org>
>Subject: Javascript Calculations
>Date: Fri, 14 Jan 2000 11:54:02 -0800
>
>Hello,
>
>I have a script that converts the numbers input in a field into a currency
>format.  I need to take 2% of that value and write it into a different
>field.  What is the most efficient way to strip that number of the "$ and
>"," characters, calculate the function, and put it back into currency
>format?  I think that I am almost there, but I can't figure out what I am
>missing to tie the two functions together.  Thanks in advance.
>
>http://www.inscodico.com/defective2.html
>
>
>

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA