mIRC Home    About    Download    Register    News    Help

Print Thread
#188818 30/10/07 09:43 PM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
hi guys, how woud i go about writing an alias for currency conversions of these 3: gbp, eur, usd

i know exchanger rates change all the time, this isn't a problem just wana know the best way of writing it?

//echo -a $currencyConvert(10.00, gbp, usd) would return the $ usd for 10 pounds

any ideas guys?

Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
One way to do this:

$CurrencyConvert(N,from,to)
possible properties: .long .unit smile

Code:
alias currencyconvert {

  ; set up the currencies (in relation to each other)
  ; format: code1 unit1 rate1,code2 unit2 rate2,...

  var %currencies = USD $ 1,GBP £ 0.4839,EUR € 0.6941,CAD C$ 0.9553,CHF sFr. 1.1627,JPY ¥ 114.8122

  ; set the floating point (round)
  var %float = 2

  var %from, %to

  ; error checking
  if ($1 !isnum 0-) { return }
  $iif(($wildtok(%currencies,$2 * *,1,44)),%from = $v1,return)
  $iif(($wildtok(%currencies,$3 * *,1,44)),%to = $v1,return)

  ; calculation and return according to properties
  return $iif(($istok($prop,long,44)),$1 $gettok(%from,1,32) to $gettok(%to,1,32) $+ :) $&
    $round($calc($1 * (1/ $gettok(%from,3,32)) * $gettok(%to,3,32)),%float) $&
    $iif(($istok($prop,unit,44)),$gettok(%to,2,32))
}


examples:
//echo -a $CurrencyConvert(2,usd,gbp)
0.97

//echo -a $CurrencyConvert(0.5,usd,chf).long
0.5 USD to CHF: 0.58

//echo -a $CurrencyConvert(700,cad,usd).unit
732.75 $

//echo -a $CurrencyConvert(5000,jpy,eur).long,unit
5000 JPY to EUR: 30.23 €

Joined: Aug 2005
Posts: 1,052
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
Here's my old script I did awhile back to convert currency I just made a quick dialog to go with it

Code:
alias currency { 
  if ($dialog(currency)) { %cur.c = $$did(currency,11) | %cur.a = $$did(currency,1) | %cur.b = $$did(currency,2) | sockopen currency www.gocurrency.com 80 }
  else { echo 4 -a ERROR: dialog is not open to calculate currency }
}
alias curr { dialog -m currency currency }

on 1:sockopen:currency:{
  .sockwrite -n currency GET $+(/v2/dorate.php?inV=,%cur.c,&from=,%cur.a,&to=,%cur.b) HTTP/1.1
  .sockwrite -n currency HOST: www.gocurrency.com
  .sockwrite -n currency $crlf
}

on 1:sockread:currency:{
  if ($sockerr > 0) return 
  var %x | sockread -fn %x
  if (Dollar(S) isin %x) { 
    did -ra currency 5 $nhtml(%x)
  }
  if ($chr(61) == $gettok(%x,3,32)) && (!%curr.d) {
    did -ra currency 7 $nhtml(%x) | %curr.d = 1 
  }
  elseif ($chr(61) == $gettok(%x,3,32)) && (%curr.d == 1) {
    did -ra currency 8 $nhtml(%x) | unset %curr.d 
  }
}

alias -l nhtml { return $remove($regsubex($1-,/(&nbsp;|&deg;|^[^<]*>|<[^>]*>|<[^>]|&nbsp;*$)/g,),$chr(9)) }

dialog currency {
  title "Currency Convertor"
  size -1 -1 307 67
  option dbu
  combo 1, 96 3 35 50, sort drop
  combo 2, 150 3 35 50, sort drop
  text "Convert:", 3, 68 3 25 8
  text "To:", 4, 134 3 13 8
  edit "", 5, 190 19 116 10, read autohs
  button "Convert", 6, 190 3 115 12, flat
  edit "", 7, 190 40 116 10, read autohs
  edit "", 8, 190 50 116 10, read autohs
  text "Exchange Rate:", 9, 227 30 41 8
  text "Amount:", 10, 5 3 22 8
  edit "", 11, 29 2 36 10
}

on *:dialog:currency:*:*:{
  if ($devent == init) {
    did -a $dname 1,2 EUR 
    did -a $dname 1,2 USD
    did -a $dname 1,2 GBP 
    did -a $dname 1,2 JPY 
    did -a $dname 1,2 INR 
    did -a $dname 1,2 NZD 
    did -a $dname 1,2 CHF 
    did -a $dname 1,2 ZAR
    did -a $dname 1,2 AFN  
    did -a $dname 1,2 ALL  
    did -a $dname 1,2 DZD 
    did -a $dname 1,2 ARS 
    did -a $dname 1,2 AUD 
    did -a $dname 1,2 ATS
    did -a $dname 1,2 BSD 
    did -a $dname 1,2 BHD 
    did -a $dname 1,2 BDT 
    did -a $dname 1,2 BBD
    did -a $dname 1,2 BEF
    did -a $dname 1,2 BMD
    did -a $dname 1,2 BRL 
    did -a $dname 1,2 BGN 
    did -a $dname 1,2 CAD  
    did -a $dname 1,2 XOF 
    did -a $dname 1,2 XAF 
    did -a $dname 1,2 CLP
    did -a $dname 1,2 CNY 
    did -a $dname 1,2 COP
    did -a $dname 1,2 XPF  
    did -a $dname 1,2 CRC 
    did -a $dname 1,2 HRK
    did -a $dname 1,2 CYP 
    did -a $dname 1,2 CZK 
    did -a $dname 1,2 DKK  
    did -a $dname 1,2 DEM  
    did -a $dname 1,2 DOP 
    did -a $dname 1,2 NLG 
    did -a $dname 1,2 XCD  
    did -a $dname 1,2 EGP  
    did -a $dname 1,2 EEK  
    did -a $dname 1,2 FJD 
    did -a $dname 1,2 XAU 
    did -a $dname 1,2 XAG
    did -a $dname 1,2 XPT 
    did -a $dname 1,2 XPD
  }
  elseif ($devent == sclick) {
    if ($did == 6) && ($did($dname,11) isnum) { currency }
  }
}


if you want just to have an alias base on what is asked with updated info all the time then this...

Code:
alias CurrencyConvert {  %cur.c = $$1 | %cur.a = $$2 | %cur.b = $$3 | sockopen currency www.gocurrency.com 80 | halt }

on 1:sockopen:currency:{
  .sockwrite -n currency GET $+(/v2/dorate.php?inV=,%cur.c,&from=,%cur.a,&to=,%cur.b) HTTP/1.1
  .sockwrite -n currency HOST: www.gocurrency.com
  .sockwrite -n currency $crlf
}

on 1:sockread:currency:{
  if ($sockerr > 0) return 
  var %x | sockread -fn %x
  if (Dollar(S) isin %x) { 
    echo -a $gettok($nhtml(%x),5-7,32)
  }
}

alias -l nhtml { return $remove($regsubex($1-,/(&nbsp;|&deg;|^[^<]*>|<[^>]*>|<[^>]|&nbsp;*$)/g,),$chr(9)) }


It will read from site using $currencyconvert(n,from,to)


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
Now I cannot add to my post...
Reading it again, maybe the easy principle I used lies a bit burried in the code I pasted. All you need is a fixed point (I used USD) and the relative rates of other items to it.

Scenario:
//var %apples = 1, %melons = 0.5, %peaches = 3
This means: for one apple, I can trade half a melon, or 3 peaches. Thats their rate. The other way round: Melons have the double value of apples, and a peach is worth 1/3 of an apple.

Now, If e.g. you want to get the exchange result trading apples and melons, you'd simply multply its number by its value:
Their value is $calc(1 / rate) ... $cac(1/0.5) == 2.
3 melons are worh $calc(2[value] * 3[number]) = 6 apples. Easy.

But how to convert peaches to melons, or vice versa? For instance, how many melons do I get for 12 peaches?

Use the "rule of proportion" and "convert" them to the "fixed point" first, to apples. In fact, we did this already above:
If 3 peaches are worth 1 apple, one peach is worh 1/3 of an apple: value = 1 / rate.

This value is now 1) multiplied by the number of items (peaches):
$calc(%num * (1 / %peaches) * %melons) ... == $calc(12 * (1/3) * 0.5)

and 2) multiplied by the rate for melons:
$calc(12 * (1/3) * 0.5) .... == $calc(12 / 3 * 0.5) ... == $calc(4 * 0.5) ... == 2

12 peaches (with a exchange rate of 3) equal 4 apples, and 1 apple is worth half a melon: 12 peaches are worth 2 melons.

Thus, the calculation goes: [number of items-to-get] == $calc([number of items-to-give] / [rate of item-to-give] * [rate of item-to-get])

Joined: Aug 2005
Posts: 1,052
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
Im hungry reading your post lol


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }
Joined: Jun 2006
Posts: 508
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Jun 2006
Posts: 508
Originally Posted By: Lpfix5
Code:
  if ($devent == init) {
    did -a $dname 1,2 EUR 
    did -a $dname 1,2 USD
<snip>

</snip>
    did -a $dname 1,2 XPT 
    did -a $dname 1,2 XPD
  }

Neater code follows wink
Code:
  if ($devent == init) {
    didtok $dname 1,2 32 EUR USD GBP JPY INR NZD CHF ZAR AFN ALL DZD ARS AUD ATS BSD BHD BDT BBD BEF BMD $&
      BRL BGN CAD XOF XAF CLP CNY COP XPF CRC HRK CYP CZK DKK DEM DOP NLG XCD EGP EEK FJD XAU XAG XPT XPD
  }



Link Copied to Clipboard