A
A
Alexander Lashchevsky2014-07-23 23:31:50
JavaScript
Alexander Lashchevsky, 2014-07-23 23:31:50

How to make automatic currency conversion against the ruble?

Hello!

There is a need to set up automatic price conversion from rubles to some others.

There are a number of blocks:

<span class="rub">1000</span> рублей
<span class="usd">????</span> долларов
<span class="eur">????</span> евро
<span class="yah">????</span> гривен
<span class="byr">????</span> белорусских рублей
It is desirable that the conversion takes place relative to the current exchange rate of the Central Bank.
How to do it?

Sincerely,
Alexander.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Lashchevsky, 2014-07-24
@Alexanevsky

Many thanks to @EnterSandman and @Taraflex for the hints. I solved the problem in the following way:
Create a PHP exchange rate parser file:

<?php 
 $content = get_content(); 
 $pattern = "#<Valute ID=\"([^\"]+)[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>[^>]+>([^<]+)[^>]+>[^>]+>([^<]+)#i"; 
 preg_match_all($pattern, $content, $out, PREG_SET_ORDER); 
 
 $usd = ""; 
 $eur = ""; 
 $byr = ""; 
 $uah = ""; 
 $kzt = ""; 

 foreach($out as $cur) 
 
 { 
   if($cur[2] == 840) $usd  = str_replace(",",".",$cur[4]); 
   if($cur[2] == 978) $eur   = str_replace(",",".",$cur[4]); 
   if($cur[2] == 974) $byr   = str_replace(",",".",$cur[4]); 
   if($cur[2] == 980) $uah   = str_replace(",",".",$cur[4]); 
   if($cur[2] == 398) $kzt   = str_replace(",",".",$cur[4]); 
 } 

 echo "<span id=\"bank-usd\">".$usd."</span> <span id=\"bank-eur\">".$eur."</span> <span id=\"bank-byr\">".$byr."</span> <span id=\"bank-uah\">".$uah."</span> <span id=\"bank-kzt\">".$kzt."</span>"; 
 
 function get_content() 

 { 
   $link = "http://www.cbr.ru/scripts/XML_daily.asp"; 
   $fd = fopen($link, "r"); 
   $text=""; 
   echo "";
   while (!feof ($fd)) $text .= fgets($fd, 4096); 
   fclose ($fd); 
   return $text; 
 }

?>
Anywhere in the HTML document, we insert the download of this file, and wrap all its contents in display: none.
We create a JS file that processes currency rates and converts our 1000 rubles into each of them:
var rub = 1000;
var bankUSD = document.getElementById('bank-usd').innerText;
var bankEUR = document.getElementById('bank-eur').innerText;
var bankBYR = document.getElementById('bank-byr').innerText;
var bankUAH = document.getElementById('bank-uah').innerText;
var bankKZT = document.getElementById('bank-kzt').innerText;

document.getElementById('rub').innerHTML = rub;
document.getElementById('usd').innerHTML = (rub/bankUSD).toFixed(2);
document.getElementById('eur').innerHTML = (rub/bankEUR).toFixed(2);
document.getElementById('byr').innerHTML = (10000*rub/bankBYR).toFixed(0);
document.getElementById('uah').innerHTML = (10*rub/bankUAH).toFixed(1);
document.getElementById('kzt').innerHTML = (100*rub/bankKZT).toFixed(0);
For the Belarusian, Kazakh and Ukrainian currencies, I had to add a few more zeros for the correct output.
And in any convenient place where we need, we output:
<span id="rub"></span> рублей
<span id="byr"></span> рублей
<span id="uah"></span> гривен
<span id="kzt"></span> тенге
<span id="usd"></span> долларов
<span id="eur"></span> евро

D
Dmitry Skogorev, 2014-07-23
@EnterSandman

I'm not strong in JS, but I'll give you direction
jsfiddle.net/Bn7X9

A
Alexander Taratin, 2014-07-24
@Taraflex

The exchange rate can be found here
www.cbr.ru/scripts/XML_daily.asp

G
Grag, 2014-07-24
@Grag

National Bank to help.
But if the resource is not Russian, you can get a lawsuit by converting the zloty to the ruble at the Russian rate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question