I have a script that calculates totals based on price, quantity, tax and shipping cost. When I attempt to add them together I receiving high numbers for total cost
Here is the script:
function calculate() {
var total = 0;
var shiptotal = 0;
var subtotal = 0;
var taxtotal = 0;
var taxrate = .078;
$('.button-click').each(function () {
var amt = parseInt($(this).prev().val());
var qty = parseInt($(this).parent().find(".quantity").val());
var ship = parseInt($(this).parent().find(".ik-ship").val());
shiptotal += (ship * qty);
subtotal += (amt * qty);
taxtotal += ( (amt * qty) * taxrate);
total += ( subtotal + shiptotal + taxtotal );
});
$('#Amount').val(total.toFixed(2));
$('.total-amount').html( total.toFixed(2) );
$('.sub-total-amount').html( subtotal.toFixed(2) );
$('.shipping-amount').html( shiptotal.toFixed(2) );
$('.tax-amount').html( taxtotal.toFixed(2) );
}
A product that cost $62.00 and has $3.00 in shipping comes out like this:
SUB-TOTAL: 62.00
SHIPPING: 3.00
TAX: 4.84
TOTAL: 1257.05 <-- incorrect total -->
I may have been in front of the computer for too long but how do I solve this problem? Please provide an example.
Use integer math by multiplying the numbers for 1000.
Related
My JavaScript is:
$('tbody').delegate('.qty,.price,.dis', 'keyup', function() {
var tr = $(this).parent().parent();
var qty = tr.find('.qty').val();
var price = tr.find('.price').val();
var dis = tr.find('.dis').val();
var amount = (qty * price) - (qty * price * dis) / 100;
tr.find('.amount').val(amount);
total();
var tax = tr.find('.tax').val();
var g_total = total() + (total() * tax) / 100;
tr.find('.g_total').val(g_total);
});
Now I want the user to be able to add sales tax and automatically give the grand total to the user. My JavaScript is working correctly until total but the rest not working.
My price: 20
Quantity: 2
Tax: 10%
How to calculate this?
I dont know the formula to do this calculation.
In your case:
var priceWithTax = 20 * 1.10 * 2;
or more general:
var priceWithTax = price * (1 + taxPercentage / 100) * amount;
The order of factors doesn't matter.
Just multiply all of them together:
const price = 20;
const quantity = 2;
const taxRate = 0.1;
const tax = price * quantity * taxRate
const totalPrice = price * quantity + tax;
var price = 20;
var tax = 0.1;
var qty = 2;
//amount includes the tax
var amountWithTax = (qty * price) / (1 - 0.1);
console.log(amountWithTax);
//amount you get after you pay the tax
var amountAfterDeductTax = amountWithTax - (amountWithTax * tax) ;
console.log(amountAfterDeductTax)
I've built a form, it works out a total, then another total, takes 4% away from the total and then gives the final value.
My issue is that i do not receive the decimal places to the calculation, so for example if the total price is £500 and the 4% removal is $52.10 my total value is 53, not 52.10.
JSFiddle: https://jsfiddle.net/znc93w90/1/
jQuery(document).ready(function(){
var insOn = 15;
var insOff = 0;
var insuranceCover = 0;
var qtyy=jQuery("#wpforms-2562-field_5");
var completetotal=jQuery("#totaltwo");
qtyy.keyup(function(){
//NewValue = 4% of the total
var newValue = parseInt(completetotal.val()) * 0.04;
//FinalValue = Total - 4% - 25
var finalValue = parseInt(completetotal.val()) - parseInt(newValue) - 25;
//Spit the totals out
jQuery('#totalfinal').text(finalValue.toFixed(2));
jQuery('#testing').text(newValue.toFixed(2));
jQuery('#testingtwo').val(finalValue.toFixed(2));
});
});
Just don't parse your numbers to an Int:
//NewValue = 4% of the total
var newValue = completetotal.val() * 0.04;
//FinalValue = Total - 4% - 25
var finalValue = completetotal.val() - newValue - 25;
I'm trying to apply a discount to a selection in JavaScript, but for some reason, my code is returning the total to subtract as the total price:
selectone = parseInt(selectone);
var textarea = document.getElementById('discount');
var word = '15off';
var textValue=textarea.value;
if (textValue.indexOf(word)!=-1)
{
var discval = parseFloat(selectone);
var num = parseInt(discval);
var retval = num - (num * .15);
} else {
var retval = 0
}
var total = selectone - retval;
document.getElementById("rettotal").innerHTML = "Price starts from £" + total;
}
For example, if something costs £100 and a 15% discount is applied, the total will be '£15' instead of '£100' ('retval' instead of 'total')
Is there something I've missed in this, or is something missing?
I've not done maths in JavaScript much so a bit over my head!
Many thanks
You've logic problem in math part.
You want to get amount after discount.
You're doing it:
var retval = num - (num * .15); // 100 - (100 * .15) = 85
But after You're removing discount from amount:
var total = selectone - retval; // 100 - 85 = 15
So here is the fix:
var price = parseFloat(selectone);
var discount = (textValue.indexOf('15off') != -1)?
price * .15
: 0;
var total = price - discount; // 100 - 15 = 85
or just be simple (if discount applies once):
var total = parseFloat(selectone);
if(textValue.indexOf('15off') != -1) {
total *= .85;
}
let's be flexible (applying multiple discounts to price):
var textValue = 'take this 15off and this 10off';
var price = parseFloat(1000);
var total = price;
total-= (textValue.indexOf('15off') != -1)?
price * .15
: 0;
console.log(total);
total-= (textValue.indexOf('10off') != -1)?
price * .15
: 0;
console.log(total);
Because... math.
selectone = parseInt(selectone);
...
var discval = parseFloat(selectone); // doesn't change the things, it's an int already
var num = parseInt(discval); // so num is essentially discval, which is selectone
var retval = num - (num * .15); // here you get 85% of num...
...
var total = selectone - retval; // here you get 15% back
The fix is to remove num - from retval, so as var retval = num * .15;
The code you've shown could be compressed to this:
var textarea = document.getElementById('discount');
var total = parseFloat(selectone)*(1-0.15*textarea.value.includes("15off"));
document.getElementById("rettotal").innerHTML = "Price starts from £" + total;
Or, if you have problems with includes() not being supported by your browser (in case it's IE), you could also use match():
var total = parseFloat(selectone)*(1-0.15*(textarea.value.match("15off")|0));
You have a JavaScript operator precedence and meaning problem there. That's syntax mistake on your part.
In an expression like this:
x - y = z
You are thinking that:
z = x - y //but it's not.
What you are really saying is:
y = z and x = x - z
Okay So I am trying to add tax into this equation if the dropdown for tax is selected as 1 or Yes My code below is what I have:
function Calculate() {
var par = $(this).parent().parent();
var price = $(par).find('input#price').val();
var tax = $(par).find('#tax').val();
if(price == ''){
return false;
}
var qty = $(this).val();
if(qty == ''){
return false;
}
if(tax == '1'){
var tax = "<?php echo $this->config->item('default_tax'); ?>";
}
else{
var tax = '0.00';
}
var total = parseFloat(price) * parseFloat(tax) * parseFloat(qty);
$(par).find('input#total').val(total.toFixed(2));
$(par).find('#Ltotal').text(total.toFixed(2));
CalculateTotal();
}
Before I added all the tax stuff, it was working just fine. But I need that tax figured in unfortunately.
Now when the above code runs I get the amount of tax on each product and not the total amount.
So for example if I have a tax rate as: 8.650%
And a line total of: $15.00
and a quantity of: 1
The script would return 1.30 for the amount. Which is the tax amount. The total should be 16.30
What am I doing wrong?
I'm going to say something is wrong with this line:
var total = parseFloat(price) * parseFloat(tax) * parseFloat(qty);
But not sure what.
The correct equation would be price * qty, then multiple times 1 + tax rate.
var total = (parseFloat(price) * parseFloat(qty)) * (1+parseFloat(tax));
You have to be careful though because javascript uses floats and this can cause some strange (unexact) measurements.