I have prepared this jsfiddle that ilustrates how my script calculate twice the sum of each selected option attribute price. Please help me to solve this issue.
optionsamount is wrong, I mean is calculated twice.. Why is that? Thanks
function update_amounts(){
var sum = 0.0;
var optionsamount = 0.0;
$('#basketorder > tbody > .product').each(function() {
$('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
}
Try this,
function update_amounts(){
var sum = 0.0;
$('#basketorder > tbody > .product').each(function() {
var optionsamount = 0.0;
$(this).find('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
try this
function update_amounts(){
var sum = 0.0;
$('#basketorder > tbody > .product').each(function() {
var optionsamount = 0.0;
$('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
}
Related
**I have a code below, i want to change/replace currency format from ($) with (Rp.) in js. I have tired with this, anybody can help me?
function update_total() {
var total = 0;
$('.price').each(function(i){
price = $(this).html().replace("$","");
if (!isNaN(price)) total += Number(price);
});
total = roundNumber(total,2);
$('#subtotal').html("$"+total);
$('#total').html("$"+total);
update_balance();
}
function update_balance() {
var due = $("#total").html().replace("$","") - $("#paid").val().replace("$","");
due = roundNumber(due,2);
$('.due').html("$"+due);
}
function update_price() {
var row = $(this).parents('.item-row');
var price = row.find('.cost').val().replace("$","") * row.find('.qty').val();
price = roundNumber(price,2);
isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("$"+price);
update_total();
}
If its only html you want to replace you can use
replace('$','Rp')
I am making a small project where a Javascript is supposed to perform price, dicsount and tax calculation. Expected calcualtion should be -
(Unit Price - Discount) + TAX
Discount could be in Percent or number.
The problem I am facing is with round off.
For Example -
Unit Price = 3.95
Discount = 5%
TAX = 28%
My javascript gives output value of 4.82 after round off.
When same thing is calculated using calculator it is showing 4.8032
It is a huge difference.
What am I doing wrong ?
My Javascript code is as follows -
function calculate_total(value) {
var id = document.getElementById('id').value;
var tot_qty=0;
var tot_vat=0;
var tot_sat=0;
var tot_disc=0;
var tot_taxable_amount = 0;
var taxable_amount = 0;
var total_amount=0;
var total_amount1=0;
var grand_total=0;
var overall_total=0;
for(i=1;i<=id;i++){
var up = document.getElementById('part_desc'+i+'_unitprice').value;
var disc = document.getElementById('part_desc'+i+'_discount').value;
up = parseFloat(up);
if(!up){
up = 0;
}
if(disc.search('%')==-1){
disc = parseFloat(disc);
var disc_symbol=0;
}
else{
disc = disc.replace('%','');
disc = parseFloat(disc);
var disc_symbol=1;
}
if(!disc){
disc=0;
}
if(disc_symbol==1){
disc = (up*disc/100);
var price = up - disc;
}
else{
var price = up-disc;
}
taxable_amount = Math.round(price*100)/100;
tot_taxable_amount += taxable_amount;
var vat = $('.part_desc'+i+'_cgst').html();
vat = vat.replace("%","");
var sat = $('.part_desc'+i+'_sgst').html();
sat = sat.replace("%","");
vat = parseFloat(vat);
//alert(vat+'-'+sat);
if(!vat){
vat = 0;
}
sat = parseFloat(sat);
if(!sat){
sat = 0;
}
vat = (Math.round((price*vat/100)*100))/100;
sat = (Math.round((price*sat/100)*100))/100;
$('.part_desc'+i+'_cgst_value').html(vat);
$('.part_desc'+i+'_cgst_value_hidden').val(vat);
$('.part_desc'+i+'_cgst_hidden').val(vat);
$('.part_desc'+i+'_sgst_value').html(sat);
$('.part_desc'+i+'_sgst_value_hidden').val(vat);
$('.part_desc'+i+'_sgst_hidden').val(vat);
price = price + vat + sat;
price = Math.round(price*100)/100;
var qty = document.getElementById('part_desc'+i+'_quantity').value;
qty = parseFloat(qty);
if(!qty){
qty = 0;
}
var total = qty * price;
total = Math.round(total*100)/100;
vat = vat*qty;
sat = sat*qty;
disc = disc*qty;
tot_qty+=qty;
tot_vat+=vat;
tot_sat+=sat;
tot_disc+=disc;
total_amount+=total;
$('.part_desc'+i+'_taxable_price').html(taxable_amount);
taxable_amount = taxable_amount*qty;
$('.part_desc'+i+'_taxable').html(taxable_amount);
$('.part_desc'+i+'_taxable_hidden').val(taxable_amount);
document.getElementById('part_desc'+i+'_eprice').value = price;
$('.part_desc'+i+'_total').html(total);
$('.part_desc'+i+'_total_hidden').val(total);
}
tot_disc = Math.round(tot_disc*100)/100;
tot_taxable_amount = Math.round(tot_taxable_amount*100)/100;
tot_vat = Math.round(tot_vat*100)/100;
tot_sat = Math.round(tot_sat*100)/100;
total_amount = Math.round(total_amount*100)/100;
$('#tot_qty').html(tot_qty);
$('#tot_disc').html(tot_disc);
$('#tot_taxable').html(tot_taxable_amount);
$('#total_cgst').html(tot_vat);
$('#total_sgst').html(tot_sat);
$('#total_amount').html(total_amount);
$('#tot_qty_hidden').val(tot_qty);
$('#tot_disc_hidden').val(tot_disc);
$('#tot_taxable_hidden').val(tot_taxable_amount);
$('#total_cgst_hidden').val(tot_vat);
$('#total_sgst_hidden').val(tot_sat);
$('#total_amount_hidden').val(total_amount);
total_amount1 = total_amount;
var labor = document.getElementById('labor').value;
var transport_charge = document.getElementById('transport_charge').value;
var expenses = 0;
labor = parseFloat(labor);
if(!labor){
labor=0;
}
transport_charge = parseFloat(transport_charge);
if(!transport_charge){
transport_charge=0;
}
expenses = labor + transport_charge;
document.getElementById('total_expenses').value = expenses.toFixed(2);
total_amount1 = total_amount1+expenses;
var round_off = total_amount1;
dummy_grand_total = total_amount1.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;
if(document.getElementById('round_off_manual').checked==false){
document.getElementById('round_off').value = round_off.toFixed(2)
total_amount1 = total_amount1.toFixed(0);
}
else{
var round_off_value = document.getElementById('round_off').value;
round_off_value = parseFloat(round_off_value);
if(!round_off_value){
round_off_value=0;
}
total_amount1 = parseFloat(total_amount1.toFixed(2))+round_off_value;
total_amount1 = total_amount1.toFixed(2);
}
$('#total_amount1').val(total_amount1);
var other_disc = document.getElementById('other_discount').value;
if(other_disc.search('%')==-1){
other_disc = parseFloat(other_disc);
var disc_symbol=0;
}
else{
other_disc = other_disc.replace('%','');
other_disc = parseFloat(other_disc);
var disc_symbol=1;
}
if(!other_disc){
other_disc=0;
}
if(disc_symbol==1){
other_disc = (total_amount1*other_disc/100);
grand_total = total_amount1 - other_disc;
}
else{
grand_total = total_amount1-other_disc;
}
var round_off = grand_total;
dummy_grand_total = grand_total.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;
if(document.getElementById('round_off_manual').checked==false){
document.getElementById('round_off_dn').value = round_off.toFixed(2)
grand_total = grand_total.toFixed(0);
}
else{
var round_off_value = document.getElementById('round_off_dn').value;
round_off_value = parseFloat(round_off_value);
if(!round_off_value){
round_off_value=0;
}
grand_total = parseFloat(total_amount1.toFixed(2))+round_off_value;
grand_total = grand_total.toFixed(2);
}
document.getElementById('grand_total').value = grand_total;
}
Thanks to #JDHrnnts
The answer to the problem is -
Save the computed data to a variable then display with Math.round, but do not modify the variable.
I'm trying to make a form that contains several different products with different options to calculate and I need to get the total price.
Scenario:
(a+b) * c = price
(a+b) * c = price
(a+b) * c = price
price + price + price = toltal
This is my script, but it doesn't work
$(document).ready(function () {
$("#form").on("keyup", ".form-calc", function () {
var parent = $(this).closest(".items");
var sum = parent.find(".form-line").val((parent.find(".form-cost").val() + parent.find(".form-cost-skift").val()).toFixed(2));
parent.find(".form-line").val((parent.find(".form-qty").val() * sum).toFixed(2));
var total = 0;
$(".form-line").each(function () {
total += parseFloat($(this).val() || 0);
});
$("#total").val(total.toFixed(2));
});
});
JSFiddle
What I'm missing?
Maybe something like this? https://jsfiddle.net/tnzcz8a3/3/
$(document).ready(function () {
$("#form").on("keyup", ".form-calc", function () {
var parent = $(this).closest(".items");
var sum = 0;
var total = 0;
if (!isNaN(parent.find(".form-cost").val())) sum += +parent.find(".form-cost").val();
if (!isNaN(parent.find(".form-cost-skift").val())) sum += +parent.find(".form-cost-skift").val();
if (!isNaN(parent.find(".form-qty").val())) sum *= +parent.find(".form-qty").val();
parent.find(".form-line").val(sum.toFixed(2));
$(".form-line").each(function () {
total += parseFloat($(this).val() || 0);
});
$("#total").val(total.toFixed(2));
});
});
I am trying to calculate all fields in the form which have the same class (shown below in the code), I have managed to get this to work, I think I may be setting the wrong value types in javascript as its not adding them together correctly. Its adding them on top of each other and not to each other. so instead of 4.00 + 5.50 equalling to 9.50 it would read 04.0005.50.
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = this.value;
//Number(this.value, 10);
if (!isNaN(num)) {
total += num.toFixed(2);
}
});
$("#total").val(total);
}
Any help its appreciated, Thanks
<script language="javascript">
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = this.value;
//Number(this.value, 10);
if (!isNaN(num)) {
total += num.parseFloat(num);
}
});
$("#total").val(total);
}
</script>
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = parseFloat(this.value);
//Number(this.value, 10);
if (!isNaN(num)) {
total += num;
}
});
$("#total").val(total);
}
I'd like to create a simple math practising js script.
For example:
The sum is given (=10) and there are four select with numbers from 0 to 10.
And if I choose a number I should disable the wrong options. I mean, the sum of selected options can not bigger then 10.
I tried this way: click here
$(document).on("change", "select.number", function(){
var sum = 10;
var number = $(this).children("option:selected").val();
var sum_number = 0;
$("select.number option:selected").each(function(){
sum_number = parseInt($(this).val(),10) + parseInt(sum_number,10);
});
$("select.number option").attr("disabled", false);
if(sum_number>0){
var act_person = act_max_person = 0;
$("select.number").each(function(){
$(this).children("option:not(:first)").each(function(){ // first is 0
if(parseInt($(this).val(),10) + parseInt(sum_number,10) > sum) $(this).attr("disabled", true);
});
});
}
});
But it's vary far from the perfect...
Try
$(document).on("change", "select.number", function () {
var sum = 10;
var number = $(this).children("option:selected").val();
var sum_number = 0;
$("select.number option:selected").each(function () {
sum_number += parseInt($(this).val(), 10);
});
$('select.number option').prop('disabled', false);
if (sum_number > 0) {
var diff = sum - sum_number;
$('select.number').each(function () {
var idx = parseInt($(this).val(), 10) + diff + 1;
$(this).find('option').slice(idx).prop('disabled', true)
})
}
});
Demo: Fiddle