Search code examples
javascriptphpjquerygoogle-finance

Calculator input change to unrounded result


Having this currency calculator, when a natural value is inserted user gets relevant currency well, however when inserted a number such as 8.5 or 4.3 etc... You get a rounded result. how do I change this and show accurate result (2 digits after the point) instead where the echo shows: Thanks (please up vote the question)

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);


     });
});

})


Solution

  • Just change this

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

    to this

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