Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: onytoo
  Where is onytoo?
 Padang
 Indonesia
 onytoo
 Tags
Subject: Another Currency Conversion Function
Thread ID: 337746 Message ID: 337746 # Views: 89 # Ratings: 15
Version: Visual FoxPro 9 SP2 Category: General VFP Topics
Date: Friday, February 24, 2012 6:47:25 PM         
   


Hi all,

I'm not sure this topic can make your attention, but I made decision to post this.

One of my friend ask me how to convert any currency to another by real time
value. I've done this couple months ago but not in VFP language, I have made it
using PHP language.

After quick conversion from PHP to VFP, then I send this function to my friend
and he said thank you cause problem solved.

Okay.... here it is.


CLEAR ALL
CLEAR
? Currency_Conversion_ECB(1000, "USD", "GBP")


*!* Currency Conversion
*!*
*!* Using ECB (European Central Bank) online rates data
*!* Parameters:
*!* - tnAmount     : Amount of money
*!* - tcFrom       : Source currency
*!* - tcTo         : Destination currency
*!* - tcClearCache : Clear download cache, set true if you want get realtime value
FUNCTION Currency_Conversion_ECB(tnAmount, tcFrom, tcTo, tlClearCache)
  LOCAL lcCurrencies, lcURL, lcTarget

  *!* just for sure
  tcFrom = UPPER(m.tcFrom)
  tcTo = UPPER(m.tcTo)

  *!* check for valid currency
  lcCurrencies = ;
    "USD JPY BGN CZK DKK GBP HUF LTL LVL PLN RON SEK CHF NOK HRK RUB TRY AUD " + ;
    "BRL CAD CNY HKD IDR ILS INR KRW MXN MYR NZD PHP SGD THB ZAR "

  IF NOT (m.tcFrom $ m.lcCurrencies)
    =MESSAGEBOX("Invalid source currency!")
    RETURN m.tnAmount
  ENDIF

  IF NOT (m.tcTo $ m.lcCurrencies)
    =MESSAGEBOX("Invalid destination currency!")
    RETURN m.tnAmount
  ENDIF

  m.lcURL    = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
  m.lcTarget = ADDBS(SYS(2023)) + SYS(2015) + ".XLS"

  DECLARE LONG URLDownloadToFile IN urlmon.dll LONG, STRING, STRING, LONG, LONG

  IF m.tlClearCache
    DECLARE LONG DeleteUrlCacheEntry IN wininet.dll STRING
    =DeleteUrlCacheEntry(m.lcURL)
  ENDIF

  IF URLDownloadToFile(0, m.lcURL, m.lcTarget, 0, 0) = 0
    LOCAL lcData, lnFromRate, lnToRate
    lcData = FILETOSTR(m.lcTarget)
    ERASE (m.lcTarget)
    *!* lookup for source rate
    lnFromRate = VAL(STREXTRACT(m.lcData, "<Cube currency='" + m.tcFrom + "' rate='", "'"))
    *!* lookup for destination rate
    lnToRate = VAL(STREXTRACT(m.lcData, "<Cube currency='" + m.tcTo + "' rate='", "'"))
    *!* now calculate and return new amount
    RETURN ((m.tnAmount / m.lnFromRate) * m.lnToRate)
  ELSE
    =MESSAGEBOX("Cannot retrieve rates data from ECB!")
  ENDIF

  RETURN m.tnAmount
ENDFUNC



Maybe this function have weakness, but you can improve it by your self.
And for another option, you can use Google finance data to do the same purpose.

Finaly... enjoy it.


Regards,
Onytoo




COMPLETE THREAD

Tip: click here to scan for Windows Registry Errors and Optimize PC performance
Another Currency Conversion Function Posted by onytoo @ 2/24/2012 6:47:25 PM
RE: Another Currency Conversion Function Posted by Cesar @ 2/24/2012 7:04:01 PM
RE: Another Currency Conversion Function Posted by onytoo @ 2/24/2012 7:07:31 PM
RE: Another Currency Conversion Function Posted by Cesar @ 2/24/2012 7:12:53 PM
RE: Another Currency Conversion Function Posted by Rahul Moudgill @ 2/25/2012 9:47:01 AM
RE: Another Currency Conversion Function Posted by Syed Imran Ulhaq @ 2/25/2012 10:54:44 AM
RE: Another Currency Conversion Function Posted by Sajan Jacob @ 2/27/2012 11:21:04 AM
RE: Another Currency Conversion Function Posted by Rajesh Malhotra @ 2/27/2012 12:08:33 PM