PDA

View Full Version : Javascript Calculator not showing correct decimals


kona72
04-10-2006, 07:39 PM
Hi All,
I have made a javascript "calculator" of sorts but i am having a bit of a problem. It is intended to show prices but when there is a number such as 24.50 it drops the zero. Any ideas?? Here is the javascript code if needed...

<script language="javascript" type="text/javascript">
<!--
function calcprice(){

a = document.getElementById('POS_Qty');
b = document.getElementById('RGO_Qty');
c = document.getElementById('STR_Qty');
d = document.getElementById('WC_Qty');
e = document.getElementById('SS_Qty');
f = document.getElementById('SC_Qty');
g = document.getElementById('MH_Qty');
h = document.getElementById('FFT_Qty');
i = document.getElementById('SER_Qty');
j = document.getElementById('WL_Qty');
k = document.getElementById('REL_Qty');
l = document.getElementById('COM_Qty');
m = document.getElementById('price');
n = document.getElementById('POS_Price');
o = document.getElementById('RGO_Price');
p = document.getElementById('STR_Price');
q = document.getElementById('WC_Price');
r = document.getElementById('SS_Price');
s = document.getElementById('SC_Price');
t = document.getElementById('MH_Price');
u = document.getElementById('FFT_Price');
v = document.getElementById('SER_Price');
w = document.getElementById('WL_Price');
x = document.getElementById('REL_Price');
y = document.getElementById('COM_Price');
z = document.getElementById('price2');
aa = document.getElementById('sh');
bb = document.getElementById('total');


n.value = parseFloat(a.value) * parseFloat(m.value);
o.value = parseFloat(b.value) * parseFloat(m.value);
p.value = parseFloat(c.value) * parseFloat(m.value);
q.value = parseFloat(d.value) * parseFloat(m.value);
r.value = parseFloat(e.value) * parseFloat(m.value);
s.value = parseFloat(f.value) * parseFloat(m.value);
t.value = parseFloat(g.value) * parseFloat(m.value);
u.value = parseFloat(h.value) * parseFloat(m.value);
v.value = parseFloat(i.value) * parseFloat(m.value);
w.value = parseFloat(j.value) * parseFloat(m.value);
x.value = parseFloat(k.value) * parseFloat(m.value);
y.value = parseFloat(l.value) * parseFloat(z.value);

val1 = parseFloat(n.value);
val2 = parseFloat(o.value);
val3 = parseFloat(p.value);
val4 = parseFloat(q.value);
val5 = parseFloat(r.value);
val6 = parseFloat(s.value);
val7 = parseFloat(t.value);
val8 = parseFloat(u.value);
val9 = parseFloat(v.value);
val10 = parseFloat(w.value);
val11 = parseFloat(x.value);
val12 = parseFloat(y.value);
val13 = parseFloat(aa.value);

st = eval(val1+val2+val3+val4+val5+val6+val7+val8+val9+ val10+val11+val12+val13);

bb.value = st;

}[/code]

ranjan
04-10-2006, 08:30 PM
Please upload the page and provide a link.

kona72
04-10-2006, 08:32 PM
it's live already...

https://www.psychovisual.com/DVD_order_form.php

Thanks!!!

ranjan
04-10-2006, 10:29 PM
javascript will not preserve 0 after decimal point. You will have to convert the result to the format you want to show like this piece of code


window.onload = function () {
var a = 22.35
var b = 22.45
sum = eval(parseFloat(a) + parseFloat(b))
var z = to_neat_decimal(sum)
alert(z)
}
function to_neat_decimal(val){
if(isNaN(val)){
alert(val+" is an invalid input");
return;
}
val=""+Math.round(100*val);
var dec_point=val.length-2;
var first_part=val.substring(0,dec_point);
var second_part=val.substring(dec_point,val.length);
var result=first_part+"."+second_part;
return result;
}