Well the Euro hit the EU today and although I'm in the UK so avoid all the hassles of changing over the contents of my wallet - I thought it would be useful, maybe, to expand on the Euro article.

The exchange rates from the old currencies to the Euro are now immutable so we can code the rates into our scripts directly - if a rate does change its likely that it would hit the news first.

So what does this mean, well if your company used to deal with Deutch Marks then, from what I have been reading, you have to convert to the Deutch Marks to Euros and then deal with that. You can read more about it here. The original article does not include the Greek Drachma.

Heres a simple program to do the conversion - by no means is this proggie as complicated as the golfs from the original Euro article but if someone needs to make a module or something then this should form a simple basis :)

use strict; use constant RATES => { 'Austrian schilling' => 13.76, 'Belgian franc' => 40.3399, 'Dutch guilder' => 2.20371, 'Finnish markka' => 5.94573, 'French franc' => 6.55957, 'German mark' => 1.95583, 'Greek drachma' => 340.75, 'Irish punt' => 0.787564, 'Italian lira' => 1936.27, 'Portuguese escudo' => 200.482, 'Spanish peseta' => 166.386 }; sub toEuro { my ($currency,$amount) = @_; if(exists(RATES->{$currency})) { return RATES->{$currency} * $amount; } return; } sub toCurrency { my ($currency,$amount) = @_; if(exists(RATES->{$currency})) { return $amount / RATES->{$currency} ; } return; } # Test output :) print toEuro('French franc',10); print "\n"; print "\n" . toCurrency('French franc',toEuro('French Franc',10));


Replies are listed 'Best First'.
Calc::Euro (Re: The Euro)
by Juerd (Abbot) on Jan 02, 2002 at 04:50 UTC

    Based on your idea, I made my own Perl Eurocalculator with the following changes:

    1. Has a cloneable object-oriented interface
    2. The euro is a currency too, so toCurrency isn't really clear => uses to_national
    3. Includes the Luxembourgian Franc (there are _12_ euro countries)
    4. Uses the official three-letter abbreviations
    5. Can handle user-defined rates
    6. Has both from_euro and to_national
    7. Update (200201021239+0100) Corrected wrong values: DEM (typo), FIM (typo), ATS

    Without further ado, here's the module:

    package Calc::Euro; use strict; use Carp; my %rates = qw( LUF 40.3399 ATS 13.7603 BEF 40.3399 NLG 2.20371 FIM 5.94573 FRF 6.55957 DEM 1.95583 GRD 340.75 IEP 0.787564 ITL 1936.27 PTE 200.482 ESP 166.386 ); sub new { my ($proto, $currency) = @_; my $rate = defined $currency && ( $rates{uc $currency} || 0 + $currency ) || ( ref $proto eq __PACKAGE__ ? $$proto : croak("Invalid currency") ); return bless \$rate, ref($proto) || $proto; } sub to_euro { my ($self, $amount) = @_; return $amount / $$self; } sub to_national { my ($self, $amount) = @_; return $amount * $$self; } sub from_euro { goto &to_national } sub from_national { goto &to_euro } sub clone { goto &new }

    And of course a script to prove it works ;)

    #!/usr/bin/perl -w use strict; use Calc::Euro; my $eurocalc = Calc::Euro->new('NLG'); print $eurocalc->to_national(1), "\n"; # 2.20371 my $anothercalc = $eurocalc->clone(); print $eurocalc->to_national(1), "\n"; # 2.20371 $eurocalc = Calc::Euro->new('BEF'); print $anothercalc->from_euro($eurocalc->to_euro(20)), "\n"; # approx 1 my $foo = Calc::Euro->new(1.5); print $foo->to_national(2), "\n"; # 3

    Have fun!

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      for the sake of simplicity, how about adding a EUR => 1 in the hash and getting rid of the National vs Euro distinction :)

      eurCalc($value, $from, $to)

      Tiago
        Well in the spirit of that request, heres an additional sub which you can add to the module. You must add the additional key value pair of 'EUR' => 1 to the rates hash or you'll have problems doing any euro conversions :)

        sub euroCalc { my ($self,$amount, $from,$to) = @_; croak("Invalid start currency") unless defined($rates{uc $from}); croak("Invalid target currency") unless defined($rates{uc $to}); # What was it in Euros # $amount = ($amount * $rates{$to}) / $rates{$from}; # Now convert to target currency # return $amount * $rates{$to}; # Or in one line return ($amount * $rates{$to}) / $rates{$from}; }


        And some test prints

        use strict; use Calc::Euro; my $eurocalc = Calc::Euro->new('NLG'); print $eurocalc->to_national(1), "\n"; # 2.20371 print $eurocalc->euroCalc($eurocalc->euroCalc(1,'NLG','EUR'),'EUR','NL +G'), "\n"; print $eurocalc->euroCalc(1,'NLG','BEF'), "\n"; print $eurocalc->euroCalc(1,'NLG','EUR'), "\n"; print $eurocalc->euroCalc(1,'EUR','NLG'), "\n";


        So you notice that you can go from one currency to Euros and then to your target currency. Might prove handy to someone.

        Have fun :)
Re: The Euro
by davorg (Chancellor) on Jan 02, 2002 at 15:16 UTC
    The exchange rates from the old currencies to the Euro are now immutable so we can code the rates into our scripts directly - if a rate does change its likely that it would hit the news first.

    Actually, the exchange rates have been immutable since Jan 1st 1999. I was writing Euro conversion programs with hard-coded values when the Euro was first introduced three years ago.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      That seems like a poor way to do it (having the rate be immutable for 2 years, that is). What is to keep a country from plunging its currency straight in to the toilet (accidentally or otherwise) only to have the exchange rate be the same as when their currency was strong? Things that make you go hmmmm...

      thor

        What actually happened was that on Jan 1st 1999, the Franc, the Mark, the Lira and the other nine currencies all ceased to exist in any real sense. At that point, the exchange rates to the Euro were fixed. All financial dealings in these currencies became dealings in the Euro. Euros were only converted to the old national currencies when you wanted physical coins and notes in your pocket. All the fuss in the news over the last few days was, in some ways, deceptive. The 12 countries have been using the Euro for three years now. The only difference this year is that notes and coins have been issued.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: The Euro
by domm (Chaplain) on Jan 02, 2002 at 15:36 UTC
    1 Euro is actually 13.7603 ATS instead of 13.76. And if you deal with 1.000.000 Euros, those 0.03 Groschen (100 Groschen=1 ATS) make a difference!
Re: The Euro
by TomK32 (Monk) on Jan 02, 2002 at 03:27 UTC
    Nice program although <s>it's</s> was Deutsch Mark not German mark

    --
    paco for president
    TomK32 - just a geek trying to change the world

      Um, no, the whole table is in English. Hence, it's "German mark" and "Italian lira", not "Deutsche Mark" and "Lira Italiano".

      I thought Deutche Mark.

      But still, three-letter ISO (DEM for DEutsche Mark) codes would be better.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        ISO 4217 if anyone is interested.