2016-07-14 37 views
-1

有了這個貨幣計算器,當插入一個自然值時,用戶獲得相關貨幣,但是當插入一個數字時,例如8.5或4.3等...您會得到一個四捨五入的結果。我該如何改變這一點,顯示出準確的結果 (點後2位),而不是在那裏回聲顯示: 謝謝(請了投票的問題)計算器輸入更改爲未被佔用的結果

PHP>

<?php 
/********************************************** 
** CONVERT TO NIS FROM EXCEL 
** OPTIONAL XML URLS 
** 
** EXAMPLES 
** - get_currency('USD', 'ILS', 15) 
** - get_currency('ILS', 'USD', 15) 
** 
**********************************************/ 
function get_currency($from_Currency, $to_Currency, $amount) { 

    $amount   = urlencode($amount); 
    $from_Currency = urlencode($from_Currency); 
    $to_Currency = urlencode($to_Currency); 
    $url   = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency"; 

    $ch    = curl_init(); 
    $timeout  = 0; 

    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

    $rawdata  = curl_exec($ch); 
    curl_close($ch); 

    $data = explode('bld>', $rawdata); 
    $data = explode($to_Currency, $data[1]); 

    return round($data[0], 2); 
} 



/********************************************** 
** RUN AJAX FUNCTION 
**********************************************/ 

    // GRAB URL 
    $result  = array(); 
    $amount  = intval($_POST['camount']); 
    $from  = substr($_POST['cfrom'], 0, 3); 
    $to   = substr($_POST['cto'], 0, 3); 

    $result[0] = get_currency($from, $to, $amount); 
    $result[1] = get_currency($from, $to, 1); 
    $result[2] = round($result[1] * 10, 2); 
    $result[3] = round($result[1] * 100, 2); 
    $result[4] = round($result[1]* 1000, 2); 

    // ECHO RESULTS 
    echo '<div id="result">'.$result[0].'</div>'; 
    echo '<div id="result_1">'.$result[1].'</div>'; 
    echo '<div id="result_10">'.$result[2].'</div>'; 
    echo '<div id="result_100">'.$result[3].'</div>'; 
    echo '<div id="result_1000">'.$result[4].'</div>'; 

?> 

的javascript>

jQuery(function($){ 

$('.btn-calculate').click(function() { 

    $('.loading').fadeIn(); 

    var camount  = $('#amount').val(); 
    var cfrom  = $('select#from option:selected').val(); 
    var cfrom_str = $('select#from option:selected').text(); 
    var cto   = $('select#to option:selected').val(); 
    var cto_str  = $('select#to option:selected').text(); 

    console.log(cfrom_str); 
    console.log(cto_str); 

    $('div#ajaxLoader').fadeIn(); 

    $.ajax({ 
     type: "POST", 
     url: "", 
     data: {camount:camount, cfrom: cfrom, cto: cto} 

    }).done(function(result) { 

     $('.loading').fadeOut(); 

     var $response   = $(result); 
     var result    = $response.filter('#result').html(); 
     var result_1   = $response.filter('#result_1').html(); 
     var result_10   = $response.filter('#result_10').html(); 
     var result_100   = $response.filter('#result_100').html(); 
     var result_1000   = $response.filter('#result_1000').html(); 

     $('.convertfrom_txt').each(function() { $(this).text(cfrom_str); }) 
     $('.convertto_txt').each(function() { $(this).text(cto_str); }) 

     $('#cresult').text(result + ' ' + cto_str); 
     $('#one_unit').text(result_1); 
     $('#ten_unit').text(result_10); 
     $('#hundred_unit').text(result_100); 
     $('#thousand_unit').text(result_1000); 


    }); 
}); 

})

+1

你應該看看到函數'toFixed()' –

+0

在哪裏,你的意思是 – eyalix

+0

在JavaScript中,如果你有一個像' 「8.4」'的值,調用'parseFloat( '8.4')。toFixed(2)'將返回'「8.40」'。 –

回答

1

只要改變這個

$amount  = intval($_POST['camount']); 

這個

$amount  = floatval($_POST['camount']);