Euro converter

Richard Suchenwirth 2002-01-03 - The following routine is not interesting for its algorithm. Two days after the introduction of Euro cash, this little Euro calculator rather commits the variety of soon extinct European currencies, and their fixed exchange rates, to history (and Tcl code):

 proc Euro {direction currency amount} {
    set usage "usage: Euro {from|to} currency amount"
    interp alias {} -> {} set f
    switch -- $currency {
        ATS - öS      {-> 13.7603}
        BEF - bfr     {-> 40.3399}
        DEM - DM      {-> 1.95583}
        ESP - Ptas    {-> 166.386}
        FIM - Fmk     {-> 5.94573}
        FRF - FF      {-> 6.55957}
        GRD - Dr      {-> 340.750}
        IEP           {-> .787564}
        ITL - Lit     {-> 1936.27}
        LUF - lfr     {-> 40.3399}
        NLG - hfl - f {-> 2.20371}
        PTE - Esc     {-> 200.482}
        default       {error "bad currency $currency - $usage"}
    }
    switch -- $direction {
        from    {set result [expr {$amount/$f}]}
        to      {set result [expr {$amount*$f}]}
        default {error "bad direction $direction - $usage"}
    }
    format %.2f $result
 }

Notice the interp sugar where "->" stands for the repeated "set f"...


Arjen Markus As a Dutchman, I do like the use of the symbol "f", as this is the now all but extinct Dutch currency symbol :-) (To those with an interest in history: the "f" comes from "florin", a name derived from the wonderful city of Florence, Italy. In fact, an old name for the Dutch guilder is "florijn". Alas, those bygone days ...) RS Added "f" as alias for NLG. (I picked the variable name "f" as abbrev for "factor", but glad you like it ;-) GWM In UK pre-decimal currency (pre 1971) the 2 shilling piece (10 pence in new money, about .15 euro) was occasionally called a florin. Bygone days? Back then we had REAL bygone days, not like these modern ones.... NEM notes that Florin and Guilder are also the names of the countries in "The Princess Bride"... [1 ].


Here is a little UI for Euro conversions, that uses the above procedure, and dynamically updates all fields when one is changed:

 set currencies {EUR ATS BEF DEM ESP FIM FRF GRD IEP ITL LUF NLG PTE}

 proc EuroUI {} {
     foreach c $::currencies {
         label .l$c -text $c -width 8
         entry .e$c -textvar $c -width 12 -justify right
         grid .l$c .e$c -sticky nws
     }
     .lEUR configure -bg white -text \u20AC
     .eEUR configure -bg yellow
     foreach event {<Key> <BackSpace>} {
         bind Entry $event {+ recompute %W}
     }
     set ::EUR 1.00
     recompute .eEUR
 }
 proc recompute w {
     set currency [string map {.e ""} $w]
     if {$currency!="EUR"} {
         catch {set ::EUR [Euro from $currency [set ::$currency]]}
     }
     foreach c [lrange $::currencies 1 end] {
         if {$c==$currency} continue
         catch {set ::$c [Euro to $c $::EUR]}
     }
 }
 if {[file tail [info script]]==[file tail $argv0]} {
     EuroUI
 } ;# RS