PHP舍入汇率转换

时间:2016-11-23 18:14:26

标签: php math rounding

为什么PHP将我的数字缩小?

我需要获得更准确的结果,而不是对数字进行舍入,我该如何实现?

这是代码;

    /// Get Exchange Rates
        $exchange_rates = "exchange_rates.xml";
        if(time()-filemtime($exchange_rates)>24*3600){
         $dom = new DOMDocument();
         $dom->load('http://www.floatrates.com/daily/gbp.xml');
         $dom->save($exchange_rates);
        }

    /// Work with the following Exchange Rates
        $exchange_rates_required = array("AUD","EUR","USD","CAD","JPY","SEK","NOK");

    /// Loop through remaining currencies and convert figure
        if(file_exists($exchange_rates)){
         $exchange_rates_xml = simplexml_load_file($exchange_rates);
         foreach($exchange_rates_xml as $val){
          if(in_array($val->targetCurrency,$exchange_rates_required)){
           $price = 10.00; 
           $rate = $val->exchangeRate; 
           echo $price . "\t " . $rate . "\t" . ($rate*$price) . "\n";
          }
         }
        }

数据;

    USD  10  1.24658358     10
    EUR  10  1.17132961     10
    CAD  10  1.67421309     10
    AUD  10  1.68093433     10
    JPY  10  138.04769510   1380
    NOK  10  10.62606016    100
    SEK  10  11.47199056    110

1 个答案:

答案 0 :(得分:0)

    /// Get Exchange Rates
        $exchange_rates = "exchange_rates.xml";
        if(time()-filemtime($exchange_rates)>24*3600){
         $dom = new DOMDocument();
         $dom->load('http://www.floatrates.com/daily/gbp.xml');
         $dom->save($exchange_rates);
        }

    /// Work with the following Exchange Rates
        $exchange_rates_required = array("AUD","EUR","USD","CAD","JPY","SEK","NOK");

    /// Loop through remaining currencies and convert figure
        if(file_exists($exchange_rates)){
         $exchange_rates_xml = simplexml_load_file($exchange_rates);
         foreach($exchange_rates_xml as $val){
          if(in_array($val->targetCurrency,$exchange_rates_required)){
           $price = number_format(10.00,6);
           $rate = ((floatval($val->exchangeRate)));
           echo $val->targetCurrency . "\t " . $price . "\t " . $rate . "\t" . ($price*$rate) . "\n";
          }
         }
        }

结果;

USD  10.000000   1.24658358 12.4658358
EUR  10.000000   1.17132961 11.7132961
CAD  10.000000   1.67421309 16.7421309
AUD  10.000000   1.68093433 16.8093433
JPY  10.000000   138.0476951    1380.476951
NOK  10.000000   10.62606016    106.2606016
SEK  10.000000   11.47199056    114.7199056