• Resolved piotr1985

    (@piotr1985)


    I’m building a calculator for but the totals are not displaying correctly. I have formulas setup for the fields and then they are populated into an html section.
    All of the calculations work correctly except for the total.
    Instead of adding the two Var amounts, it’s placing them next to each other instead.

    Below is the code for the calculation function. The totalmins, totalhrs, etc. are the calculations that give the incorrect amount.

    Is there a way to get those calculations to display correctly within the HTML?
    Thanks!

    (function(){
    var annual = (fieldname19*(fieldname13/100)),
    revlostweek = (annual/52),
    revlostweek1 = (PREC(revlostweek,2)),
    revlostday = (PREC(annual/365,2)),
    revlosthour = (PREC(annual/8760,2)),
    revlostmin = (PREC(annual/525600,2)),
    productivity = (fieldname20/100),
    prodlosthour1 = ((fieldname3*fieldname11)*productivity),
    prodlosthour2 = (PREC(prodlosthour1*1,2)),
    prodlostmin = (PREC(prodlosthour2/60,2)),
    prodlostday = (PREC(prodlosthour2*24,2)),
    prodlostweek = (PREC(prodlosthour2*168,2)),
    totalmins = (revlostmin+prodlostmin),
    totalhrs1 = (PREC(revlosthour+prodlosthour2,2)),
    totalday1 = ((revlostday)+(prodlostday)),
    totalweek1 = (revlostweek + prodlostweek);
    
    jQuery('.revlost_week').html(revlostweek);
    jQuery('.revlost_day').html(revlostday);
    jQuery('.revlost_hour').html(revlosthour);
    jQuery('.revlost_min').html(revlostmin);
    jQuery('.prodlost_hour').html(prodlosthour2);
    jQuery('.prodlost_min').html(prodlostmin);
    jQuery('.prodlost_day').html(prodlostday);
    jQuery('.prodlost_week').html(prodlostweek);
    jQuery('.total_mins').html(totalmins);
    jQuery('.total_hours').html(totalhrs1);
    jQuery('.total_days').html(totalday1);
    jQuery('.total_week').html(totalweek1);
    })()

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @piotr1985

    In javascript the plus (+) operator is overloaded, it is used for summing numbers and concatenate texts, if any of the operands is a text, the concatenation takes precedence.

    Furthermore, when you call the PREC operation in this way: revlostday = (PREC(annual/365,2)), you are taking a number, and transforming it into a text with the format: ######.##

    So, as the value of the operand revlostday is a text, in the code: totalday1 = ((revlostday)+(prodlostday)), the concatenation takes precedence, and concatenates both operands.

    There are different alternatives:

    – Apply the PREC operation at the end, when the value is displayed:

    
    jQuery('.revlost_day').html(PREC(revlostday,2));
    

    Generating the variable simply as: revlostday = annual/365,

    – Or you can use the SUM operation instead the plus operator:

    
    (function(){
    var annual = (fieldname19*(fieldname13/100)),
    revlostweek = (annual/52),
    revlostweek1 = (PREC(revlostweek,2)),
    revlostday = (PREC(annual/365,2)),
    revlosthour = (PREC(annual/8760,2)),
    revlostmin = (PREC(annual/525600,2)),
    productivity = (fieldname20/100),
    prodlosthour1 = ((fieldname3*fieldname11)*productivity),
    prodlosthour2 = (PREC(prodlosthour1*1,2)),
    prodlostmin = (PREC(prodlosthour2/60,2)),
    prodlostday = (PREC(prodlosthour2*24,2)),
    prodlostweek = (PREC(prodlosthour2*168,2)),
    totalmins = SUM(revlostmin, prodlostmin),
    totalhrs1 = PREC(SUM(revlosthour,prodlosthour2),2),
    totalday1 = SUM(revlostday, prodlostday),
    totalweek1 = SUM(revlostweek, prodlostweek);
    
    jQuery('.revlost_week').html(revlostweek);
    jQuery('.revlost_day').html(revlostday);
    jQuery('.revlost_hour').html(revlosthour);
    jQuery('.revlost_min').html(revlostmin);
    jQuery('.prodlost_hour').html(prodlosthour2);
    jQuery('.prodlost_min').html(prodlostmin);
    jQuery('.prodlost_day').html(prodlostday);
    jQuery('.prodlost_week').html(prodlostweek);
    jQuery('.total_mins').html(totalmins);
    jQuery('.total_hours').html(totalhrs1);
    jQuery('.total_days').html(totalday1);
    jQuery('.total_week').html(totalweek1);
    })()
    

    Best regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Vars are not adding correctly’ is closed to new replies.