Discussion of Forex Trading and Currency Trading

how do you program a currency converter?


Most Commented Posts

 

 

3 Responses to “how do you program a currency converter?”

  1. I use this one http://www.xe.com/ucc/
    You don't need to program it. You just set the parameters (amount to be converted, originating currency and currency you want to convert to

  2. Greetings Austin,

    Below is the source code of a currency converter that can be executed using Windows Messenger Live's MSN Plus Add-on. Hope it can serve as a source of inspiration. Once executed you could try modifying the code and taking it apart to see what makes it tick

    ~Rhetticus

    /*
    Currency translator
    Auther : Martin Kirk (mdk@mail.dk)
    Date 01-okt 2006

    Version 3.1
    */

    var engNames = new Array();
    engNames["AUD"] = "Australian dollars";
    engNames["BGN"] = "Bulgarian lev";
    engNames["CAD"] = "Canadian dollars";
    engNames["CHF"] = "Swiss franc";
    engNames["CNY"] = "Chinese yuan renminbi";
    engNames["CYP"] = "Cyprus Pound";
    engNames["CZK"] = "Czech Koruna";
    engNames["DKK"] = "Danish Kroner";
    engNames["EEK"] = "Estonian kroon";
    engNames["EUR"] = "Euro";
    engNames["HKD"] = "Hong Kong dollars";
    engNames["HRK"] = "Croatian kuna";
    engNames["HUF"] = "Hungarian forint";
    engNames["IDR"] = "Indonesian rupiah";
    engNames["ISK"] = "Icelandic krona";
    engNames["JPY"] = "Japanese Yen";
    engNames["GBP"] = "Pound Sterling";
    engNames["KRW"] = "South Korean won";
    engNames["LTL"] = "Lithuanian litas";
    engNames["LVL"] = "Latvian lats";
    engNames["MTL"] = "Maltese lira";
    engNames["MYR"] = "Malaysian ringgit";
    engNames["NOK"] = "Norwegian krone";
    engNames["NZD"] = "New Zealand dollars";
    engNames["PHP"] = "Philippine peso";
    engNames["PLN"] = "Polish zloty";
    engNames["RON"] = "New Romanian leu";
    engNames["RUB"] = "Russian rouble";
    engNames["SEK"] = "Swedish kroner";
    engNames["SKK"] = "Slovak koruna";
    engNames["SGD"] = "Singapore dollars";
    engNames["THB"] = "Thai baht";
    engNames["SIT"] = "Slovenian tolar";
    engNames["TRY"] = "New Turkish lira";
    engNames["USD"] = "US Dollar";
    engNames["ZAR"] = "South African rand";

    var xmlhttp = null;
    var arr = new Array(); // stores currencys array(ISO,Rate,Descr)
    arr[0] = new Array("DKK",100,"Danish Kroner");

    var DefaultOut = "DKK";

    //fill the array
    loadXMLDoc("http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml");

    //sort the array by ISO, assending
    arr.sort();

    function loadXMLDoc(url){
    Debug.Trace("loading XML");

    //xmlhttp=new XMLHttpRequest();
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET",url,true);

    xmlhttp.onreadystatechange = function(){
    if( xmlhttp.readystate==4 ){
    //Debug.Trace(xmlhttp.responseText);

    var response = xmlhttp.responseXML.documentElement;
    var x = response.getElementsByTagName('currency');

    for (i=0;i<x.length;i++){
    arr[i+1] = new Array(
    x[i].getAttribute('code'),
    x[i].getAttribute('rate').replace(".","").replace(",","."),
    x[i].getAttribute('desc') );
    Debug.Trace(arr[i][0]+", "+arr[i][1]+", "+arr[i][2]);
    }

    }//if
    }//function
    xmlhttp.send(null);
    }

    function OnGetScriptCommands()
    {
    var ScriptCommands = "<ScriptCommands>";
    ScriptCommands += "<Command>";
    ScriptCommands += "<Name>Currency</Name>";
    ScriptCommands += "<Description>Translate currency, Default is to 'Danish Kroner'</Description>";
    ScriptCommands += "<Parameters>< Amount > < ISO > < destination ISO *"+DefaultOut+" is default ></Parameters>";
    ScriptCommands += "</Command>";
    ScriptCommands += "<Command>";
    ScriptCommands += "<Name>Currencies_Set_Default</Name>";
    ScriptCommands += "<Description>Set what default output you want on simple expressions (those without 3rd param.)</Description>";
    ScriptCommands += "<Parameters>< destination ISO ></Parameters>";
    ScriptCommands += "</Command>";
    ScriptCommands += "<Command>";
    ScriptCommands += "<Name>Currencies_Available</Name>";
    ScriptCommands += "<Description>Displays available ISO currencies</Description>";
    ScriptCommands += "<Parameters>";
    for(i in arr)
    ScriptCommands+= arr[i][0] + ", ";
    ScriptCommands += "</Parameters>";
    ScriptCommands += "</Command>";
    ScriptCommands += "</ScriptCommands>";

    return ScriptCommands;
    }
    function translate(from,to,amount){
    return Math.round( ( (from/to) * amount) * 100) / 100;
    }

    function Decode(amount,iso_f,iso_t,option){
    var cur1, cur2; //Currency
    var curName1, curName2;//Currency Name

    for(i=0;i<arr.length;i++){
    if(arr[i][0] == iso_f){
    cur1 = arr[i][1];
    curName1 = engNames[arr[i][0]];
    }
    }
    for(i=0;i<arr.length;i++){
    if(arr[i][0] == iso_t){
    cur2 = arr[i][1];
    curName2 = engNames[arr[i][0]];
    }
    }

    Debug.Trace(iso_f+" : "+iso_t+" : "+amount+" : "+option);
    Debug.Trace(cur1 +" : "+cur2 +" : "+amount+" : "+curName2);
    if(!option)
    return translate(cur1,cur2,amount) + " " + curName2 ;
    else
    return translate(cur1,cur2,amount) + " " + iso_t ;
    }

    function OnEvent_ChatWndSendMessage(ChatWindow, Messege){
    if ((Messege.toUpperCase()).indexOf("/CURRENCIES_AVAILABLE ") == 0){
    MsgPlus.DisplayToast

  3. You need a table that will hold the conversion rates. So if you want to convert from dollars to yen, you take the conversion rate and multiply the numbers or divide to convert it back. There are websites to find current conversion rates but if you are doing this for a client, I would leave it in their hands to maintain the rates.

Leave a Reply