计算器输入更改为未结算的结果

时间:2016-07-14 13:54:09

标签: javascript php jquery google-finance

使用此货币计算器,当插入自然值时,用户可以很好地获得相关货币, 但是当插入一个数字,如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&gt;

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 个答案:

答案 0 :(得分:1)

只需更改此

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

到这个

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