How do I convert 10 to .10 or 10% in javascript? - 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);
}

Related

multiple a variable with number and adding the result again to the variable is returning wrong result [duplicate]

This question already has answers here:
Plus operator in JavaScript doesn't sum my values
(4 answers)
Closed 3 years ago.
function calculateAmount(val) {
var price = val;
if (quantity >= 100 && quantity < 1000) {
var divobj = document.getElementById('discount');
divobj.value = 20;
var divobj1 = document.getElementById('total');
var total = ((20 / 100) * parseInt(price)) + parseInt(price);
divobj1.value = total;
}
}
For example if enter the quantity to be 100 then the output return is 10020 instead 100+20 = 120
var price = val;
because the val is a string:
var price = +val;
please test this code, I hope this code works
divobj.value = 20;
please delete above line code:
var total = ((divobj.value / 100) * parseInt(price)) + parseInt(price);
and use above line code instead of use below code:
var total = ((20 / 100) * parseInt(price)) + parseInt(price);

How to calculate price and quantity with tax in 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)

Check value and run calculation in javascript

I am using javascript to calculate the tax of the products. I am using the following script for it:
<script>
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
}
</script>
Now I want to extend this script. I added three more textboxes. In the first textbox I want to add only the calculation for where the tax='10', in the second I want to add only where the tax='20' and in the third I want to add only where the tax='25'.
I need to make a script like this:
var totalValue3 = IF numval='10'(numVal1 / '100' * numVal6) + IF numval='10'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='20'(numVal1 / '100' * numVal6) + IF numval='20'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='25'(numVal1 / '100' * numVal6) + IF numval='25'(numVal2 / '100' * numVal7)
But this script is not working.
Does someone know how I can make a calculation for this?
JSfiddle:
https://jsfiddle.net/mm2fLne9/
Update:
I think this is the code you are looking for.
https://jsfiddle.net/mm2fLne9/8/
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
var totalValue3 = (numVal6 ==10 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==10 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax1").value = totalValue3.toFixed(2);
var totalValue4 = (numVal6 ==20 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==20 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax2").value = totalValue4.toFixed(2);
}

get array(s) from jquery / javascript function

I've got a javascript function which generates and returns a new array (of arrays):
function getFees(id){
var prep = new Array, primary = new Array, secondary = new Array, vce = new Array;
prep[0] = 733;
primary[0] = 792;
secondary[0] = 879;
vce[0] = 1108;
if (id == 2) {
prep[1] = (prep[0] - prep[0] * 5 / 100);
prep[1] = Math.ceil(prep[1]);
primary[1] = (primary[0] - primary[0] * 5 / 100);
primary[1] = Math.ceil(primary[1]);
secondary[1] = (secondary[0] - secondary[0] * 5 / 100);
secondary[1] = Math.floor(secondary[1]);
vce[1] = (vce[0] - vce[0] * 5 / 100);
vce[1] = Math.floor(vce[1]);
} else if (id == 3) {
prep[2] = (prep[0] - prep[0] * 10 / 100);
prep[2] = Math.ceil(prep[2]);
primary[2] = (primary[0] - primary[0] * 10 / 100);
primary[2] = Math.ceil(primary[2]);
secondary[2] = (secondary[0] - secondary[0] * 10 / 100);
secondary[2] = Math.floor(secondary[2]);
vce[2] = (vce[0] - vce[0] * 10 / 100);
vce[2] = Math.floor(vce[2]);
} else if (id == 4) {
prep[3] = (prep[0] - prep[0] * 50 / 100);
prep[3] = Math.ceil(prep[3]);
primary[3] = (primary[0] - primary[0] * 50 / 100);
primary[3] = Math.ceil(primary[3]);
secondary[3] = (secondary[0] - secondary[0] * 50 / 100);
secondary[3] = Math.ceil(secondary[3]);
vce[3] = (vce[0] - vce[0] * 50 / 100);
vce[3] = Math.floor(vce[3]);
} else if (id >= 5) {
prep[4] = (prep[0] - prep[0] * 75 / 100);
prep[4] = Math.floor(prep[4]);
primary[4] = (primary[0] - primary[0] * 75 / 100);
primary[4] = Math.ceil(primary[4]);
secondary[4] = (secondary[0] - secondary[0] * 75 / 100);
secondary[4] = Math.ceil(secondary[4]);
vce[4] = (vce[0] - vce[0] * 75 / 100);
vce[4] = Math.floor(vce[4]);
}
var newArray = [];
newArray.push({'prep':prep}); //prep array = 733,697
newArray.push({'primary':primary}); //primary array = 792,753
newArray.push({'secondary':secondary}); //secondary array = 879,835
newArray.push({'vce':vce}); //vce array = 1108,1052
return newArray;
}
Essentially I've given an example in the .push sections at the bottom. I then call my function by doing this:
var fees = getFees(2);
alert(fees);
Which alerts this:
[object Object],[object Object],[object Object],[object Object]
If I do:
alert(fees.toSource());
I get this:
[{prep:[733, 697]}, {primary:[792, 753]}, {secondary:[879, 835]}, {vce:[1108, 1052]}]
What I need to be able to do is get the number from any of the items (prep/primary/secondary/vce) eg..
fees.prep[0];
fees.primary[1];
But when I try that, I get this error:
TypeError: fees.prep is undefined
What am I missing?? Any help would be greatly appreciated!! :)
you need to access like this
fees[0].prep[0];

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

Categories

Resources