Currency Exchange

February 27, 2015

Instead of writing a program, the easiest way to complete the task is to use wget to fetch the data and awk to format it:

$ cat >currency
wget -qO- "http://rate-exchange.appspot.com/currency?from=$1&to=$2" |
awk -vAMT="$3" -F'[ ,]' '{printf "%.2f", $5 * AMT}'
CTRL-D
$ chmod +x currency
$ ./currency USD GBP 1000
644.24

The currency command takes three parameters: the from currency, the to currency, and the from amount; currency is specified by standard three-letter codes. The flags to wget specify quiet mode with output (that’s a capital-letter oh, not a zero) to standard output. Output from the wget command looks like this:

{"to": "GBP", "rate": 0.64380199999999999, "from": "USD"}

The awk command splits the output into fields; the fifth field is the conversion rate, and simple multiplication does the rest.

The trick here is the url that provides the data, which I found by googling for “currency exchange api”. With baseball season starting in a few weeks, I will soon be googling for “baseball standings api”.

Pages: 1 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: