How to calculate price and quantity with tax in javascript - javascript

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)

Related

I am creating a bill. I am taking quantity, price and dis(discount) from user and automatically calculating amount and total price

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.

Jquery Calculation - Decimals

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;

JavaScript function "X - Y = Z" returns Y as Z value

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

Adding Total Not Adding Correctly

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.

How do I convert 10 to .10 or 10% in javascript?

The third-party is feeding me 10 percent as the number 10.
In my code how do I move the decimal 2 places over for the variable itemDiscountPercent?
if (boolActive)
{
itemDiscountPercent = Math.percent(ogCookie.products[i].discount_percent);
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
if (itemQty > 1)
{
itemDiscountPercent = itemDiscountPercent * itemQty;
}
priceDiscount = itemPrice * itemDiscountPercent;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}
So instead of getting 299.8 for a line item with quantity 2, I need it to be 2.99 so I can subtract it later in the code.
divide by 100.
var dec = itemDiscountPercent / 100;
if (boolActive)
{
itemDiscountPercent = ogCookie.products[i].discount_percent;
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
//priceDiscount = itemPrice * itemQty * itemDiscountPercent / 100;
priceDiscount = Math.round(itemPrice * itemQty * itemDiscountPercent - 0.5) / 100;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}

Categories

Resources