I have this code:
function calculate_sale_article(id){
var article_price = $("#article_price_"+id).val()
var article_cant = $("#article_cant_"+id).val()
var article_discount = $("#article_discount_"+id).val()
$("#article_ammount").val(article_price * article_cant / article_discount)
}
Error: "Infinity"
This error is when article_discount arrives with a 0, how can I fix this?
Add a simple check for zero (exception handling for divide by zero). Replace the below line:
$("#article_ammount").val(article_price * article_cant / article_discount)
With something like this:
if (article_discount != 0)
$("#article_ammount").val(article_price * article_cant / article_discount)
else
$("#article_ammount").val(article_price * article_cant)
var article_price = parseInt($("#article_price_"+id).val());
var article_cant = parseInt($("#article_cant_"+id).val());
var discount = parseInt($("#article_discount_"+id).val());
var article_discount = discount?discount:1;
$("#article_ammount").val(article_price * article_cant / article_discount)
Related
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);
On my pricing page, I've got a pricing estimator; input an order value and a return rate (%) and price estimate will print into a div block below. The formula should look like this: ((orderValue * 0.05, 2) + 4) + ((returnRate / 100) * 4)... Can anyone see why this isn't working?
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
elOrderValue.addEventListener('keyup', _ => {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
if (isNaN(curValFloat)) {
elDeliveryPrice.innerHTML = '';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4) + (parseInt("elReturnRate") / 100)) * 4;
});
There are a few things that cause your issues. For starters you should also listen to 'keyup' on the returnRate input box. Secondly you should handle NaN of the returnRate value.
For example:
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
function calc() {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
let curRate = parseFloat(elReturnRate.value);
if (isNaN(curValFloat) || isNaN(curRate)) {
elDeliveryPrice.innerHTML = '£ _';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4 + (curRate / 100) * 4);
}
elOrderValue.addEventListener('keyup', calc);
elReturnRate.addEventListener('keyup', calc);
You can divide a percent number by 100 to get the correct number to include in your calculation. If the return rate input is given in text, you can convert to a number using parseInt(returnRate) e.g.
Math.max(curValFloat * 0.05,2) + 4 + (parseInt(returnRate) / 100) * 4
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
What's the problem in using number properties? I'm trying to do a simple calculation involving numbers and it returns NaN.
function test () {
var that = this;
this.usersCount = 2;
this.totalSeeds = 10;
this.test2 = function () {
console.log(2/10*100); // 20
console.log(that.usersCount * that.totalSeeds); // 20
var percentUsersCount = that.usersCount / that.totalseeds * 100; // also tried parseInt() and Number()
console.log(percentUsersCount); // NaN -- WHY !?!
}
}
var test = new test();
test.test2();
var test1 = 2;
var test2 = 10;
var percent = test1 / test2 * 100;
console.log(percent); // 20
Why is percentUsersCount NaN?
http://jsfiddle.net/ht2rv1ea/
Edit: https://www.youtube.com/watch?v=yI6VXlNRrI0
You misspelled the name of your variable. Use that.totalSeeds instead of that.totalseeds.
You have a typo:
var percentUsersCount = that.usersCount / that.totalseeds * 100;
Change to
var percentUsersCount = that.usersCount / that.totalSeeds * 100;
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);
}