I need parse this xml:
<ValCurs Date="25.04.2017" name="Foreign Currency Market">
<Valute ID="R01010">
<NumCode>036</NumCode>
<CharCode>AUD</CharCode>
<Nominal>1</Nominal>
<Name>AU Dollar</Name>
<Value>42,4465</Value>
</Valute>
<Valute ID="R01060">
<NumCode>051</NumCode>
<CharCode>AMD</CharCode>
<Nominal>100</Nominal>
<Name>AM dram</Name>
<Value>11,5747</Value>
</Valute>
...
Have next code:
$file = simplexml_load_file($link);
foreach ($file as $el) {
(float)$rates=(float)($el->Value)/(float)($el->Nominal);
echo (float)($el->Value) . '/' .(float)($el->Nominal) . '=' . $rates;
}
This code return:
32/1=32 71/1=71 11/100=0.11 29/1=29 31/1=31 17/1=17 19/100=0.19 72/10=7.2 81/10=8.1 56/1=56 60/1=60 87/100=0.87
But i need: 42,4465/1=42,4465 11,5747/100=0,115747 How to do it? )
php -v
PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
No need to cast the needed elements to float
type twice.
Use the following approach:
$sxe = simplexml_load_file($link);
foreach ($sxe as $el) {
echo "{$el->Value}/{$el->Nominal}" . "="
. bcdiv(str_replace(',', '.', $el->Value), $el->Nominal, 6) . PHP_EOL;
}
The output:
42,4465/1=42.446500
11,5747/100=0.115747
Used function: http://php.net/manual/en/function.bcdiv.php