javascript thread: Javascript calculations...
Hi,
I have this situation regarding client side Javascript.
I use this script to do some calculations with the numbers taken from some
input type text forms (some of the inputs are dynamically generated with
parts of their name also dynamically generated and some of the inputs are
static with their names static).
This is placed in HEAD:
------------------------
<SCRIPT language=Javascript>
function amount() {
var amount=0;
for(i=0;i<document.invoice.length;i++)
{
if (document.invoice[i].name.substring(0,5)=="price")
{
amount = amount + parseFloat(document.invoice[i].value);
}
}
amount = amount + parseFloat(document.invoice.shipping.value);
document.invoice.result.value = amount;
tax=Math.floor(7*amount)/100; // trick to keep the last 2 decimals but
it's not working
total = amount - tax;
document.invoice.tax.value=tax;
document.invoice.total.value=total;
}
</SCRIPT>
------------------------
And here are the input forms:
------------------------
<!-- this is the dynamic generated input - its value is dynamically
generated but also a part of its name -->
<input type="text" name="price_9" class=input_price value="24,00"
onChange="suma()">
<!-- here is finishing -->
<!-- this is the dynamic generated input - its value is dynamically
generated but also a part of its name -->
<input type="text" name="price_5" class=input_price value="133,50"
onChange="suma()">
<!-- here is finishing -->
<!-- this is the dynamic generated input - its value is dynamically
generated but also a part of its name -->
<input type="text" name="price_11" class=input_price value="24,00"
onChange="suma()">
<!-- here is finishing -->
<!-- This is the static input - its name it's static and its value -->
<input type="text" name="shipping" class=input_price value="3,90"
onChange="suma()">
<!-- here is finishing -->
<!-- this is another static input where the first total is calculated -
first using ASP and second using Javascript - the name it's static but
the value it's dynamically generated -->
<input type="text" name="result" class=input_result value="331,50" >
<!-- here is finishing -->
<!-- this is the input which it displays the tax deducted from the first
total - first it's calculated with ASP and then Javascript , its the name
is static but the value dynamic -->
<input type="text" name="tax" class=input_price value="21,69" >
<!-- here is finishing -->
<!-- this is the input where the second total, the final total, is
displayed after the tax has been deducted - first it's calculated using
ASP and then Javascript -, its the name is static but the value dynamic -->
<input type="text" name="total" class=input_price value="309,81">
<!-- here is finishing -->
-----------------------------
This script is working almost but I have some problems with it:
- if I'm changing a value in the dynamically generated fields the first
total (in the the "results" input) shows up as an integer (the
calculations are correct but it doesn't shows me the decimals which should
appears when you do operation with integers whcih have decimals - I only
need the fisrt two decimals)
- another problem is at the inputs "tax" and "total".Here the numbers are
displayed with decimals - not only with two decimals (I need only two) but
many more.How can I manage to have only two decimals? And the total result
it's also dispaled with "." between integer and decimals instead
of ",".How can I manage to have ","?
Can somebody point me in the right direction regarding this Javascript?
I can send the HTML page if necessary to see how it looks.
Thanks,
Mircea