Handle currencies with one-directional quotes and quotes < 1 in F::Q.

Some currencies quotes are one-directional, so check both directions if
necessary. Quotes with values < 1 often have too few significant digits,
so in that case use the other direction if available.
This commit is contained in:
John Ralls 2015-09-01 14:47:40 -07:00
parent ac34d316a5
commit 923b01e269
2 changed files with 22 additions and 0 deletions

View File

@ -166,6 +166,15 @@ if ($exchange eq "currency") {
while ($#ARGV >= 0) {
my $to = shift;
my $result = $q->currency($from, $to);
unless (defined($result) && $result >= 1) {
my $inv_res = $q->currency($to, $from);
if (defined($inv_res)) {
my $tmp = $to;
$to = $from;
$from = $tmp;
$result = $inv_res;
}
}
if (defined($result)) {
printf "1 $from = $result $to\n";
} else {

View File

@ -350,6 +350,19 @@ while(<>) {
last unless $to_currency;
my $price = $quoter->currency($from_currency, $to_currency);
my $inv_price = undef;
#Sometimes price quotes are available in only one direction, and if the
#direction we asked for results in a quote < 1 we want the other direction
#if it's available to get more significant digits.
unless (defined($price) && $price > 1) {
$inv_price = $quoter->currency($to_currency, $from_currency);
if (defined($inv_price)) {
my $tmp = $to_currency;
$to_currency = $from_currency;
$from_currency = $tmp;
$price = $inv_price;
}
}
$quote_data{$from_currency, "success"} = defined($price);
$quote_data{$from_currency, "symbol"} = $from_currency;