...

View Full Version : Javascript rounding amount



FatCodeMonkey
07-31-2008, 09:26 PM
The tax amount on an order form is rounding up whereas I would like for it to show the exact amount.

Here is the code:


function calculatePrice(){
var temp = 0;
if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
document.getElementById("tax_hide").value=temp.toFixed(2);
}
else
{
temp = 0;
document.getElementById("tax_hide").value=0;
}
document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);
}
window.onload = function(){setInterval(calculatePrice, 100);};


If you need to see it in action here is a link to one of the products on the site: http://www.expressprintdesign.com/2008/bcards.php. Just order a business card as an example. Once you hit submit and get to the order form page, indicate under the client information that the state is Texas, which will activate the taxation on the product. You will then see that the tax rounds up on the price.

What do I need to do to the script to fix this?

Thanks.

ninnypants
07-31-2008, 09:37 PM
Try this it will round your price to two decimal places by timesing it by 100 to put the numbers you want to keep before the decimal then rounding and moving the numbers back behind the decimal.


var tax = Math.round(cost*0.0825*100)/100;
var total = Math.round(cost+tax*100)/100; // returns price

FatCodeMonkey
07-31-2008, 09:50 PM
Hi ninnypants,
Thank you for your help. I am not the great with scripting. Where would I put this code you recommended?

ninnypants
07-31-2008, 09:56 PM
Use it to replace the red text:

if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
document.getElementById("tax_hide").value=temp.toFixed(2);
}


Just make sure all of your input is going in correctly like your price

oesxyl
07-31-2008, 10:10 PM
Try this it will round your price to two decimal places by timesing it by 100 to put the numbers you want to keep before the decimal then rounding and moving the numbers back behind the decimal.


var tax = Math.round(cost*0.0825*100)/100;
var total = Math.round(cost+tax*100)/100; // returns price

I guess, you don't need tax:


var total = Math.round(cost*108.25)/100;


I'm wrong, :) tax is used in form so don't help to remove itregards

FatCodeMonkey
07-31-2008, 10:59 PM
Hi ninnypants,
I replaced the line as you recommended, but now the tax field does not change as it is supposed to.. ??

ninnypants
07-31-2008, 11:08 PM
document.getElementById("total_hide").value = total;
document.getElementById("tax").value ='$ '+tax;
document.getElementById("total").value ='$ '+total;


That should update your form fields or what I'm guessing are form fields

FatCodeMonkey
07-31-2008, 11:48 PM
Do I replace some script with that or just add it under the other one?

ninnypants
08-01-2008, 12:12 AM
This section:

document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);

FatCodeMonkey
08-01-2008, 12:49 AM
I replaced that section with the other one and now the tax and the total fields come back 'undefined'

ninnypants
08-01-2008, 01:27 AM
Try alerting your variables at different points to see where they become undefined then we'll know where to look

rangana
08-01-2008, 02:48 AM
I was'nt able to see the page you provided a link, but basing on your script, remove all instances of toFixed() (http://www.w3schools.com/jsref/jsref_tofixed.asp):


function calculatePrice(){
var temp = 0;
if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
document.getElementById("tax_hide").value=temp.toFixed(2);
}
else
{
temp = 0;
document.getElementById("tax_hide").value=0;
}
document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);
}


Hope that does'nt cause a huge mess.

ninnypants
08-01-2008, 03:19 AM
rangana is right you also need to replace that has the toFixed() with something like where it displays the tax

FatCodeMonkey
08-01-2008, 03:27 AM
Hi Rangana,
I remove the toFixed instances, but that removed the decimal and 2 places after it under Tax and Total. I need it to show the decimal point with 2 places after it.

NOTE: the funny thing is that with the original script, when the data is transferred to the database, it shows the exact amount - only on the order form is shows the rounded amount. ?!?

rangana
08-01-2008, 04:01 AM
Use this:


<script type="text/javascript">
function calculatePrice(){
var temp = 0;
if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
temp = Number(document.getElementById("sub_hide").value) * 0.0825;
document.getElementById("tax_hide").value=temp;
}
else
{
temp = 0;
document.getElementById("tax_hide").value=0;
}
document.getElementById("total_hide").value = (Number(document.getElementById("sub_hide").value) + temp);
document.getElementById("tax").value ='$ '+(Number(document.getElementById('tax_hide').value));
document.getElementById("total").value ='$ '+(Number(document.getElementById('total_hide').value));
}
</script>

FatCodeMonkey
08-01-2008, 04:10 AM
OK, now it is calculating correctly - however, the only problem is that it shows 3 digits after the decimal point instead of 2... what can we do to fix this?

ninnypants
08-01-2008, 05:48 AM
Try this I made some changes. I think it will reduce it to two decimal places.


<script type="text/javascript">
function calculatePrice(){
var temp = 0;
if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
temp = Math.round(Number(document.getElementById("sub_hide").value) * 0.0825*100)/100;
document.getElementById("tax_hide").value=temp;
}
else
{
temp = 0;
document.getElementById("tax_hide").value=0;
}
document.getElementById("total_hide").value = (Number(document.getElementById("sub_hide").value) + temp);
document.getElementById("tax").value ='$ '+(Number(document.getElementById('tax_hide').value));
document.getElementById("total").value ='$ '+(Number(document.getElementById('total_hide').value));
}
</script>

FatCodeMonkey
08-01-2008, 04:50 PM
AWESOME!!!!
It works! :thumbsup:

Thank you both Ninnypants and Rangana for your persistent help in the matter. It is people like you that make me come to this forum.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum