Skip to main content
Topic: Decimal Doesnt Work (Read 1297 times) previous topic - next topic

Decimal Doesnt Work

hi there i have a problem here. i try to use client event in my phprad , it should calculate a decimal data type,  my database data type are already decimal 7,2
change the value step to 0.01

but when ever i put the decimal number from 1 field to another , the calculation doesnt work.





here is my script
$('#ctrl-eftpos, #ctrl-app_payment, #ctrl-discount, #ctrl-cash').on('input', function(){


    var $eftpos = $(this).closest('form').find('#ctrl-eftpos').val();
    var $app_payment = $(this).closest('form').find('#ctrl-app_payment').val();
    var $discount = $(this).closest('form').find('#ctrl-discount').val();
    var $cash = $(this).closest('form').find('#ctrl-cash').val();
   
    var $gross_sales_dom = $(this).closest('form').find('#ctrl-gross_sales');
   
    $gross_sales = ($eftpos + $app_payment + $discount + $cash);
   
    $gross_sales_dom.val($gross_sales);
   
});

any help or explanation will be much appreciated

thanks

Re: Decimal Doesnt Work

Reply #1
@guchan‍ what you are doing is not a calculation when dealing with strings in javascript, it is concatenation. If you want to do calculations you will have to use the javascript parseFloat() for decimal numbers or parseInt() for integers. Read more here parseFloat() - JavaScript | MDN (mozilla.org) and here parseInt() - JavaScript | MDN (mozilla.org).

Example
Code: [Select]
$gross_sales = parseFloat($eftpos) + parseFloat($app_payment) + parseFloat($discount) + parseFloat($cash);

Re: Decimal Doesnt Work

Reply #2
Quote
$('#ctrl-eftpos, #ctrl-app_payment, #ctrl-discount, #ctrl-cash').on('input', function(){


    var $eftpos = $(this).closest('form').find('#ctrl-eftpos').val();
    var $app_payment = $(this).closest('form').find('#ctrl-app_payment').val();
    var $discount = $(this).closest('form').find('#ctrl-discount').val();
    var $cash = $(this).closest('form').find('#ctrl-cash').val();
   
    var $gross_sales_dom = $(this).closest('form').find('#ctrl-gross_sales');
   
    $gross_sales = parseFloat($eftpos) + parseFloat($app_payment) + parseFloat($discount) + parseFloat($cash);
   
    $gross_sales_dom.val($gross_sales);

is this the correct way to do it ?

 

Re: Decimal Doesnt Work

Reply #3
found the answer thanks @willvin
$('#ctrl-eftpos, #ctrl-app_payment, #ctrl-discount, #ctrl-cash').on('input', function(){


    var $eftpos = $(this).closest('form').find('#ctrl-eftpos').val();
    var $app_payment = $(this).closest('form').find('#ctrl-app_payment').val();
    var $discount = $(this).closest('form').find('#ctrl-discount').val();
    var $cash = $(this).closest('form').find('#ctrl-cash').val();
   
    var $gross_sales_dom = $(this).closest('form').find('#ctrl-gross_sales');
   
    $gross_sales = (parseFloat($eftpos) + parseFloat($app_payment) + parseFloat($discount) + parseFloat($cash));
   
    $gross_sales_dom.val($gross_sales);
   
});