2016-11-23 89 views
0

爲什麼PHP將我的數字降低?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"; 
      } 
     } 
     } 

Data;

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 

回答

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