Forum Moderators: open

Message Too Old, No Replies

Currency code

Javascript currency

         

CyberJ

12:26 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



Hi there! could som1 please help me out? i need toknow if there's something wrong with this javascript code. The code is meant to give the american dollar and european equivalent of a price. i.e when i type a price in pounds, it gives me the price in dollars and euros. Thanks in advance

function fix(thenumber,noplaces){ // returns the number to n decimal places
var oldnumber=thenumber;
thenumber=thenumber+"0000";
var decpos=thenumber.indexOf(".");
if (decpos==-1) return oldnumber+".00";
decpos=decpos+noplaces+1;
var endstring=thenumber.substring(0,decpos);
return endstring;
}

function convert_currency(){
//var dollarval=document.converter.original.value/ConversionFactor(document.converter.sourcecurrency.value);
//document.converter.converted.value=fix(dollarval*ConversionFactor(document.converter.targetcurrency.value),2);

var dollarval= document.form.gbp.value;
document.form.usd.value=fix(dollarval*1.85,2);
document.form.eur.value=fix(dollarval*1.46,2);

}

function convert_currency_spec(){
//var dollarval=document.converter.original.value/ConversionFactor(document.converter.sourcecurrency.value);
//document.converter.converted.value=fix(dollarval*ConversionFactor(document.converter.targetcurrency.value),2);

var dollarval= document.form.special_offer_original_gbp.value;
document.form.special_offer_original_usd.value=fix(dollarval*1.85,2);
document.form.special_offer_original_eur.value=fix(dollarval*1.46,2);

}

penders

4:06 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In order to get it to work I had to change:
document.form

to:

document.forms[0]

Other than that it seemed to work OK. This references the first form on the page. If you have more than 1 form, however, this could get problematic. Best to use the form name, as in:

document.forms['myform'] (...) 
<form name="myform" (...)

Pretty good rate of exchange!

Added:
Just checking.... you are not rounding your results, you are just 'chopping off' after the 2nd decimal place. Is that ok? ie. 5.099999 becomes 5.09, whereas it should be 5.10 to two decimal places.