I am trying to round up the .qty field to the nearest whole number. I am really unsure where to place this in the below code snippet? I take it I should be using Math.ceil()?
function (){
var sm = parseFloat($(this).val());
var tsm = parseFloat($('.tsm', $(this).parent().parent()).val());
var calc = (sm*tsm); // total tiles needed
if($('.addwaste', $(this).parent().parent()).is(':checked')){
var onepercent = calc/100;
var sevenpercent = Math.ceil(onepercent*7);
calc+=sevenpercent;
}
$('.qty', $(this).parent().parent()).val(calc);
$('div.product form.cart .qty').val( calc );
var rawprice = parseFloat($('.rawprice', $(this).parent().parent()).val());
var total = (rawprice*calc).toFixed(2);
$('.total', $(this).parent().parent()).html(total);
$('div.product .price .amount').html( '£' + total );
}
this can be done by using basic javascript:
either use:
Math.floor(number here); <- this rounds it DOWN so 4.6 becomes 4
Math.round(number here); <- this rounds it UP so 4.6 becomes 5
its either floor and round OR Floor and Round.
so with your code it would be:
function (){
var sm = parseFloat($(this).val());
var tsm = parseFloat($('.tsm', $(this).parent().parent()).val());
var calc = (sm*tsm); // total tiles needed
if($('.addwaste', $(this).parent().parent()).is(':checked')){
var onepercent = calc/100;
var sevenpercent = Math.ceil(onepercent*7);
calc+=sevenpercent;
}
calc = Math.round(calc);
$('.qty', $(this).parent().parent()).val(calc);
$('div.product form.cart .qty').val( calc );
var rawprice = parseFloat($('.rawprice', $(this).parent().parent()).val());
var total = (rawprice*calc).toFixed(2);
$('.total', $(this).parent().parent()).html(total);
$('div.product .price .amount').html( '£' + total );
}
Related
I have a function where you can click to like, and you kan also click to dislike. How can I make it NOT possible to go to negative number, so that if you click dislike after 0 it stays 0?
function stem(id) {
var antal_stemmer = document.getElementById(id).className;
var nyt_antal_stemmer =+ antal_stemmer + +1;
document.getElementById(id).className = nyt_antal_stemmer;
var indhold = document.getElementsByClassName(id);
var indholdA = indhold[0].innerHTML;
indhold[0].innerHTML = nyt_antal_stemmer + " stemmer";
}
function fjern(id) {
var antal_stemmer = document.getElementById(id).className;
var nyt_antal_stemmer =+ antal_stemmer - +1;
document.getElementById(id).className = nyt_antal_stemmer;
var indhold = document.getElementsByClassName(id);
var indholdA = indhold[0].innerHTML;
indhold[0].innerHTML = nyt_antal_stemmer + " stemmer";
}
You could get the maximum of the value or zero. The result is a number greater or equal zero.
var nyt_antal_stemmer = Math.max(0, antal_stemmer - 1); // - converts the operands to number
I have a Javascript code as written below. Everything is working fine except the fact the net value that is the grand total of addition of all the total values, is coming as NaN. I have used parseFloat() but still the result is NaN. But I am getting the all the total values.
Any help is welcome.
window.onkeyup=function() {
var items = document.querySelectorAll(".item");
var itemsArray = Array.prototype.slice.call(items,0);
var unit, rate, total, net, tax, margin, rateamount = 0;
itemsArray.forEach(function(el){
unit = el.querySelector('input[name="unit[]"]').value;
rate = el.querySelector('input[name="rate[]"]').value;
tax = el.querySelector('input[name="tax[]"]').value;
margin = el.querySelector('input[name="margin[]"]').value;
el.querySelector('input[id="marginrate[]"]').options[el.querySelector('input[id="marginrate[]"]').selectedIndex].text;
var rateMargin =el.querySelector('[name="marginrate[]"]').selectedIndex;
if (rateMargin==1) {rateamount=margin/100}
if (rateMargin==0) {rateamount=margin}
total = (parseFloat(unit * rate) + parseFloat(rateamount))-parseFloat(tax/100);
alert(total);
el.querySelector('input[name="total[]"]').value = total;
net+= parseFloat(total);
});
document.getElementById('net').value=net;
}
You never initialized net, so when you do net += parseFloat(total); you're adding a number to undefined, which results in NaN. You need to initialize it to 0.
You also should be calling parseFloat on the values that are read from the inputs, not on the results of arithmetic operations (they always return numbers, you don't need to parse them).
window.onkeyup = function() {
var items = document.querySelectorAll(".item");
var itemsArray = Array.prototype.slice.call(items, 0);
var unit, rate, total, net = 0, tax, margin, rateamount = 0;
itemsArray.forEach(function(el) {
unit = el.querySelector('input[name="unit[]"]').value;
rate = el.querySelector('input[name="rate[]"]').value;
tax = el.querySelector('input[name="tax[]"]').value;
margin = el.querySelector('input[name="margin[]"]').value;
el.querySelector('input[id="marginrate[]"]').options[el.querySelector('input[id="marginrate[]"]').selectedIndex].text;
var rateMargin = el.querySelector('[name="marginrate[]"]').selectedIndex;
if (rateMargin == 1) {
rateamount = margin / 100
} else if (rateMargin == 0) {
rateamount = margin
}
total = (parseFloat(unit) * parseFloat(rate) + parseFloat(rateamount)) - parseFloat(tax) / 100;
alert(total);
el.querySelector('input[name="total[]"]').value = total;
net += total;
});
document.getElementById('net').value = net;
}
I want to push calculate sum those after Line total: display in input not in text.
I try add some input for example:
<input id="totalline" name="totalline" value="" />
and change this:
var calculate = function(el) {
var percent = el.find('input[name="percent[]"]').val();
var additional = el.find('input[name="additional[]"]').val();
var total = el.find('span.total');
var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
total.text(totalValue);
}
to this:
var calculate = function(el) {
var percent = el.find('input[name="percent[]"]').val();
var additional = el.find('input[name="additional[]"]').val();
var total = el.find('span.total');
var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
$('#totalline').val(totalValue); //<-this line changed
}
But this is not work like I want to.
Here is my fiddle.
You are trying to use an ID selector when there will be multiple elements with the same ID on the page (when you press the + button to add a new row). Simply change your selector to var total = el.find("[name='totalline']"); to ensure that you are always grabbing the correct input.
This is what it should look like:
HTML
Line total: <input name="totalline" value="" />
JS
var calculate = function(el) {
var percent = el.find('input[name="percent[]"]').val();
var additional = el.find('input[name="additional[]"]').val();
var total = el.find("[name='totalline']");
var totalValue = ($("#weight").val() * percent / (100 - additional)).toFixed(2);
total.val(totalValue);
}
I'm trying to make a financial calculation, but there's something wrong.
JS Code:
function count (){
var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;
var result = (codc/codx(codx-((codx+codc)*-codb)))*coda;
alert(result);
}
Message: undefined
In this line
var result = (codc/codx(codx-((codx+codc)*-codb)))*coda;
You try to execute the 2nd codx as a function (codx()). I guess you miss an operand there.
Try for example:
var result = (codc/codx / (codx-((codx+codc)*-codb)))*coda;
You are missing a * operator, so the compiler tries to call codx as a function.
To fix your computation, add the * operator as follow:
function count (){
var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;
var result = (codc/codx * (codx - ((codx+codc)*-codb)))*coda;
// ^
alert(result);
}
Missing * symbol? codx is being used as a fuction as a result.
var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;
var result = (codc/codx*(codx-((codx+codc)*-codb)))*coda;
alert(result);
Slightly off-topic but you should never use floating point arithmetic in financial calculations. Instead calculate by integer cents and then format for viewing as you like.
In this case,
It is better to split the mathematical calculation.
Example:
function count (){
var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;
var res1 = ((codx+codc)*-codb);
var res2 = codx-res1;
var result = (codc/codx*res2)*coda;
alert(result);
}
var count = function () {
var coda = 1.5; var codb = 15; var codc = 0.06; var codx = 1;
var result = (codc/codx ** here ** (codx-((codx+codc)* - codb))) * coda;
console.log(result);
}
P.S you have to put something after codx it's seen by javascript as an undefined function.
If this relates to paying down a loan at interest i per payment period, then you get that after n payments at a rate of r the reduced principal is
p*(1+i)^n-r*(1+i)^n-r*(1+i)^(n-1)-...-r*(1+i)
=
p*(1+i)^n - (1+i)*((1+i)^n-1)/i*r
If that is to be zero, loan repaid, then the necessary rate computes as
r = i/((1+i)*(1-(1+i)^(-n))) * p
which is in some aspects similar, in other fundamental aspects different from your formula.
var p = 1.500;
var n = 15;
var i = 0.06;
var x = 1+i;
var result = i/( x*( 1-Math.pow(x,-n) ) )*p;
alert(result);
im trying to display the 2 decimal point of the 2 total number and minus them but it didnt compute the decimal point. anyone would like to figure this out. thanks.
function calculate() {
var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var basicpay = document.getElementById('basicpay');
var myResult = myBox1 * myBox2;
basicpay.value = myResult.toFixed(2);
document.getElementById('both').value = sum() - diff();
}
this is the diff part
function diff() {
var absent = document.getElementById('absent').value;
var tardiness = document.getElementById('tardiness').value;
var sss = document.getElementById('sss').value;
var pagibig = document.getElementById('pagibig').value;
var philhealth = document.getElementById('philhealth').value;
var cashadvances = document.getElementById('cashadvances').value;
var withholdingtax = document.getElementById('withholdingtax').value;
var others = document.getElementById('others').value;
var result =
parseInt(absent) +
parseInt(tardiness) +
parseInt(sss) +
parseInt(pagibig) +
parseInt(philhealth) +
parseInt(cashadvances) +
parseInt(withholdingtax) +
parseInt(others) || 0;
if (!isNaN(result)) {
document.getElementById('totaldeductions').value = result.toFixed(2);
return result;
}
}
this is the sum part
function sum() {
var basicpay = document.getElementById('basicpay').value;
var overtime = document.getElementById('overtime').value;
var regularholiday = document.getElementById('regularholiday').value;
var specialholiday = document.getElementById('specialholiday').value;
var allowanceday = document.getElementById('allowanceday').value;
var monthpay = document.getElementById('monthpay').value;
var others1 = document.getElementById('others1').value;
var result =
parseInt(basicpay) +
parseInt(overtime) +
parseInt(regularholiday) +
parseInt(specialholiday) +
parseInt(allowanceday) +
parseInt(monthpay) +
parseInt(others1) || 0;
if (!isNaN(result)) {
document.getElementById('totalgrosspay').value = result.toFixed(2);
return result;
}
}
In your Sum() and Diff() function, you are working only with integers. Integers are whole numbers only, so will not retain anything after a decimal point. To deal with decimals, you will need to use JavaScript's parseFloat() function. To give an example, in your Sum() function you would change the result calculation to look like the following:
var result =
parseFloat(basicpay) +
parseFloat(overtime) +
parseFloat(regularholiday) +
parseFloat(specialholiday) +
parseFloat(allowanceday) +
parseFloat(monthpay) +
parseFloat(others1) || 0;
This will retain the decimal points in the numbers rather than truncating to whole numbers as the parseInt()