我需要解析这个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>
...
有下一个代码:
$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;
}
此代码返回:
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
但我需要: 42,4465 / 1 = 42,4465 11,5747 / 100 = 0,115747 怎么做? )
php -v
PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
答案 0 :(得分:1)
无需将所需元素强制转换为float
两次类型
使用以下方法:
$sxe = simplexml_load_file($link);
foreach ($sxe as $el) {
echo "{$el->Value}/{$el->Nominal}" . "="
. bcdiv(str_replace(',', '.', $el->Value), $el->Nominal, 6) . PHP_EOL;
}
输出:
42,4465/1=42.446500
11,5747/100=0.115747