RobsonLittle Red Book ▸ Currency Conversion #1

Chapter 2 - Question 4

Write programs to convert pounds sterling to other major currencies.

  1. United States Dollars
  2. French Francs
  3. Dutch Guilders
  4. Austrian Schillings
  5. Danish Krona

Input is to be a number representing a quantity in pounds, sterling; output is to be equivalent amount and the currency unit involved.

Solution 1

I decided to expand this problem to make it more challenging. I'd already written a C++ Measurements Converter, so I had no wish to just write another one. That would be too simple.

I wanted to obtain the exchange rates from the web, so writing this solution in PHP seemed ideal. To be polite I should cache the results to avoid using up loads of someone's bandwidth. After stumbling around various policy-filled sites, I eventually found the Bank of England. They allows you to build a list of currencies and then download them in a variety of formats. Very useful!

Here's my PHP solution:

<?    // * DATA OBTAINING        // filename to use for storing exchange rates    define('RATES_FILE', $_SERVER['DOCUMENT_ROOT'] . '/littleredbook/rates-cache.csv');      // check if the rates file is a day old    // content is cached to avoid hammering the server    // with this method we only make one request every 24 hours    if (!file_exists(RATES_FILE) || @filemtime(RATES_FILE) < strtotime('yesterday'))    {        // make up the static part of the url        $url = ''             . 'Travel=NIxIRxSUx&FromSeries=1&ToSeries=50&DAT=RNG&'             . 'csv.x=20&csv.y=27&CSVF=TN&FN=N&C=ECH&C=C8J&C=C8N&C=ECU&C=C8P';        // add yesterdays date        $url .= '&FD=' . date('d', strtotime('yesterday'))              . '&FM=' . date('M', strtotime('yesterday'))              . '&FY=' . date('Y', strtotime('yesterday'));        // add todays date        $url .= '&TD=' . date('d') . '&TM=' . date('M') . '&TY=' . date('Y');        // get the exchange rates and store them        $csv = file_get_contents($url);        // store locally        $fp = fopen(RATES_FILE, 'wb');        // write the rates to a file        fwrite($fp, $csv);        // close the file so anything else can use it        fclose($fp);    }    else        // if it's still new, we can use the cached version        $csv = file_get_contents(RATES_FILE);            // make up a random amount of great british pounds    $gbp = mt_rand(1, 100);            // * DATA MANIPULATION      // these are the currency names    $currency = array('Danish Krone', 'Euro', 'Japanese Yen', 'Swiss Franc', 'American Dollar');        // extract all the values into an array and return the rates from the end    $rates = array_slice(explode(',', $csv), -count($currency));        // * DATA OUTPUT        // show how many great british pounds we have    echo $gbp . ' GBP is:<ul>';    // loop through all available currency    for ($n = 0; $n < count($currency); $n++)        // show how much this is when converted        echo '<li>' . round($rates[$n] * $gbp, 2) . ' ' . $currency[$n] . '</li>';    // end the list    echo '</ul>';     ?>        

The amount is generated randomly because I don't want people to use my site as a currency converter. Here's the script in action:

57 GBP is:
  • 0 Danish Krone
  • 0 Euro
  • 0 Japanese Yen
  • 0 Swiss Franc
  • 0 American Dollar

You can refresh for a different amount.


© 2004-20 robson | cc unless stated