Currency Converter script

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
brentj84062
Posts: 18
Joined: Wed Jul 12, 2006 4:40 am

Currency Converter script

Post by brentj84062 » Sat Apr 30, 2011 10:48 pm

Hi All,

I'm working currency conversion into one of my apps and figured I'd share a free way to convert between currencies using live data from Google. Credit is due to oohyeah's Blog (google search it, I can't post links to domains/pages). Hopefully it saves someone a few minutes on their apps.

Code: Select all

function convertCurrency amount, startCur, endCur
   -- Cleanup input values before assembling the URL --
   put urlEncode(amount) into amount
   put urlEncode(startCur) into startCur -- USD, EUR, AUD, etc.
   put urlEncode(endCur) into endCur
   
   -- Create our URL to retrieve the data from Google --
   put "hl=en&q=" & amount & startCur & "%3D%3F" & endCur into theURL
   put "http://google.com/ig/calculator?" & theURL into theURL
   get url theURL
   
   -- The data comes back in JSON format. Below isn't elegant, but gets the job done
    -- To see what's going on here, I'd recommend you look at split and the raw data of the URL call
   split it using quote 
   split it[4] using space
   return it[4][1]
end convertCurrency
Thanks,
Brent Anderson
Last edited by brentj84062 on Mon May 16, 2011 6:08 pm, edited 1 time in total.

interactbooks
Posts: 65
Joined: Thu Oct 07, 2010 4:47 pm

Re: Currency Converter script

Post by interactbooks » Sun May 01, 2011 9:06 am

Thanks Brent this definitely could come in handy.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Currency Converter script

Post by bn » Sun May 01, 2011 10:33 am

Hi Brent,

very nifty, especially the use of split. Will try to use that to parse html. Thank you.

The only problem with this is that I found out that 0 Euros = 0 Dollars. I always suspected it but now it is deceptively clear. :) I don't want to get into the fact that they correctly convert negative numbers. ;)

Kind regards

Bernd

PS you would want to try to post scripts using the "Code" tab in the editor of this forum. Just click the tab and appears and you paste your code in between.

brentj84062
Posts: 18
Joined: Wed Jul 12, 2006 4:40 am

Re: Currency Converter script

Post by brentj84062 » Mon May 16, 2011 6:09 pm

Thanks for the suggestion, it's been updated to be more clipboard-friendly.

Post Reply