...

View Full Version : Currency Converter



Dead Man's Eye
05-11-2007, 05:11 PM
I'm working on a stock exchange that has a foreign exchange section. How would I code a currency converter with fixed exchange rates onto my site?

coothead
05-11-2007, 06:16 PM
Hi there Dead Man's Eye,

and a warm welcome to these forums. :)

Here is a javascript example...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>currency convertor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container {
width:320px;
padding:5px 0 10px 10px;
border:3px double #000;
font-family:verdana,arial,hevetica,sans-serif;
font-size:16px;
color:#000;
background-color:#eee;
margin:auto;
}
#container h1 {
font-size:16px;
text-align:center;
}
#container label {
display:block;
margin:5px 0;
}
#container input {
width:100px;
}
</style>

<script type="text/javascript">

window.onload=function() {
var rate=0.761672633;
var df=document.forms[0];
document.getElementById('button').onclick=function() {
rate=parseFloat(df[0].value);

if((rate==0)||(rate=='')||(isNaN(rate))) {
alert('please enter a conversion rate');
return;
}
if((isNaN(df[1].value))||(isNaN(df[2].value))||(df[1].value=='')&&(df[2].value=='')) {
alert('please enter a numerical value');
df.reset();
df[0].value=rate
return;
}
if((df[1].value!='')&&(df[2].value!='')){
alert('you can only do one calculation at a time');
df.reset();
return;
}
if(df[2].value=='') {
df[3].value='\u20ac '+parseFloat(df[1].value*rate).toFixed(2)
df[2].disabled=true;
df[2].style.backgroundColor='#ccc';
}
if(df[1].value=='') {
df[3].value='\u0024 '+parseFloat(df[2].value/rate).toFixed(2);
df[1].disabled=true;
df[1].style.backgroundColor='#ccc';
}
}
document.getElementById('clear').onclick=function() {
df[1].disabled=false;
df[2].disabled=false;
df[1].style.backgroundColor='#fff';
df[2].style.backgroundColor='#fff';
df.reset();
if(rate!=0.761672633){
df[0].value=rate;
}
}
}
</script>

</head>
<body>

<form action="#">

<div id="container">
<h1>change conversion rate to suit</h1>
<label><input type="text" value="0.761672633"/> : dollar to euro rate</label>
<label><input type="text" value=""/> : dollars to euros</label>
<label><input type="text"value=""/> : euros to dollars</label>
<label id="deg"><input type="text" readonly="readonly"/> : converted value</label>
<input id="button" type="button" value="convert"/>
<input id="clear" type="button" value="reset"/>
</div>
</form>

</body>
</html>
coothead

Dead Man's Eye
05-11-2007, 08:27 PM
Thank you. My goal I to have one similar to the full converter located here (http://www.xe.com) with fixed rates. Would I just input the desired rate for each currency?

A problem I came across is after switching it to convert the Cerulean Pound to the En (true name for ehat wee call the "yen") it won't convert.

The code:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>currency convertor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container {
width:320px;
padding:5px 0 10px 10px;
border:3px double #000;
font-family:verdana,arial,hevetica,sans-serif;
font-size:16px;
color:#000;
background-color:#eee;
margin:auto;
}
#container h1 {
font-size:16px;
text-align:center;
}
#container label {
display:block;
margin:5px 0;
}
#container input {
width:100px;
}
</style>

<script type="text/javascript">

window.onload=function() {
var rate=150.00;
var df=document.forms[0];
document.getElementById('button').onclick=function() {
rate=parseFloat(df[0].value);

if((rate==0)||(rate=='')||(isNaN(rate))) {
alert('please enter a conversion rate');
return;
}
if((isNaN(df[1].value))||(isNaN(df[2].value))||(df[1].value=='')&&(df[2].value=='')) {
alert('please enter a numerical value');
df.reset();
df[0].value=rate
return;
}
if((df[1].value!='')&&(df[2].value!='')){
alert('you can only do one calculation at a time');
df.reset();
return;
}
if(df[2].value=='') {
df[3].value='\u20ac '+parseFloat(df[1].value*rate).toFixed(2)
df[2].disabled=true;
df[2].style.backgroundColor='#ccc';
}
if(df[1].value=='') {
df[3].value='\u0024 '+parseFloat(df[2].value/rate).toFixed(2);
df[1].disabled=true;
df[1].style.backgroundColor='#ccc';
}
}
document.getElementById('clear').onclick=function() {
df[1].disabled=false;
df[2].disabled=false;
df[1].style.backgroundColor='#fff';
df[2].style.backgroundColor='#fff';
df.reset();
if(rate!=150.00){
df[0].value=rate;
}
}
}
</script>

</head>
<body>

<form action="#">

<div id="container">
<h1>change conversion rate to suit</h1>
<label><input type="text" value="150.00"/> : Pound to En rate</label>
<label><input type="text" value=""/> : pounds to en</label>
<label><input type="text"value=""/> : en to pound</label>
<label id="deg"><input type="text" readonly="readonly"/> : converted value</label>
<input id="button" type="button" value="convert"/>
<input id="clear" type="button" value="reset"/>
</div>
</form>

</body>
</html>

What's wrong?

Dead Man's Eye
05-14-2007, 12:54 AM
(blank, changed my mind)

Dead Man's Eye
05-14-2007, 12:55 AM
Why won't my code work? It should work so the person converting is able to select a rate from a list.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>currency convertor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container {
width:320px;
padding:5px 0 10px 10px;
border:3px double #000;
font-family:verdana,arial,hevetica,sans-serif;
font-size:16px;
color:#000;
background-color:#eee;
margin:auto;
}
#container h1 {
font-size:16px;
text-align:center;
}
#container label {
display:block;
margin:5px 0;
}
#container input {
width:100px;
}
</style>

<script type="text/javascript">

window.onload=function() {
var rate=;
var df=document.forms[0];
document.getElementById('button').onclick=function() {
rate=parseFloat(df[0].value);

if((rate==0)||(rate=='')||(isNaN(rate))) {
alert('please enter a conversion rate');
return;
}
if((isNaN(df[1].value))||(isNaN(df[2].value))||(df[1].value=='')&&(df[2].value=='')) {
alert('please enter a numerical value');
df.reset();
df[0].value=rate
return;
}
if((df[1].value!='')&&(df[2].value!='')){
alert('you can only do one calculation at a time');
df.reset();
return;
}
if(df[2].value=='0.00') {
df[3].value='\u20ac '+parseFloat(df[1].value*rate).toFixed(2)
df[2].disabled=true;
df[2].style.backgroundColor='#ccc';
}
if(df[1].value=='') {
df[3].value='\u0024 '+parseFloat(df[2].value/rate).toFixed(2);
df[1].disabled=true;
df[1].style.backgroundColor='#ccc';
}
}
document.getElementById('clear').onclick=function() {
df[1].disabled=false;
df[2].disabled=false;
df[1].style.backgroundColor='#fff';
df[2].style.backgroundColor='#fff';
df.reset();
if(rate!=0.00){
df[0].value=rate;
}
}
}
</script>

</head>
<body>

<form action="#">

<div id="container">
<h1>change conversion rate to suit</h1>
<label><input type="text" value="0.00"/> : Rate</label>
<label><input type="text" value=""/> : SCS to Foreign</label>
<label><input type="text"value=""/> : Foreign to SCS</label>
<label id="deg"><input type="text" readonly="readonly"/> : converted value</label>
<input id="button" type="button" value="convert"/>
<input id="clear" type="button" value="reset"/>
</div>
</form>

</body>
</html>

Dead Man's Eye
05-17-2007, 03:45 PM
Better yet, Coothead, would you please tweak yours to have a list of rates instead of just one rate?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum