# Convert string in to double

if  function Sum(fNum) , I hope to Sum 16 double values from text field but I get Sumed string.
etc. 5+6=56 , I need 5+6=11;

<script>
function Sum(fNum) {

window.document["edit"+fNum].vrednost.value=window.document["cmb"+fNum].select.value;

window.document.suma.vrednost.value=0;
for (var i=1;i<17;i++) {
window.document.suma.vrednost.value+=window.document["edit"+i].vrednost.value;
}
}
</script>
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT ExpertCommented:

val=0;
for (var i=1;i<17;i++) {
val+=+=parseInt(document.suma.elements["edit"+i].vrednost.value,10)

}
document.suma.vrednost.value=val.toFixed(2); // leave out the .toFixed(2) if you don't want it.

parseInt if integers
parseFloat(...) if decimals
IT ExpertCommented:
oops
val+=+=parseInt(document.suma.elements["edit"+i].vrednost.value,10)

should of course be
val+=parseInt(document.suma.elements["edit"+i].vrednost.value,10)
Commented:
hi there.

you can also use Number

<script>
var num1='1';
var num2='20';
var num3='13';
var num4='91';

alert( Number(num1) + Number(num2)+ Number(num3)+ Number(num4));
</script>

Nushi.
Author Commented:
Thanks a lot mplungjan, but I lost decimal places for all operands . etc 165,75 rounds to 165 , it's a big diferent for 16 operands . . . have idea how to fix that?
Commented:
thats beacuse its a parseInt
use parseDecimal

Commented:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<title>Untitled</title>

<body>
Ofcurse you will use your names/ids for the text fields.
its only a sample for the double sumerize.

<br><br>
<input type='text' value='10.5' name='aa'><br>
<input type='text' value='100' name='aa'><br>
<input type='text' value='10.34' name='aa'><br>
<input type='text' value='34.34' name='aa'><br>
<input type='text' value='1.00' name='aa'><br>
<br><br>
<input type="button" onclick="sumValues()" value='Sum Values'>
<script>
function sumValues(){

var fileds = document.getElementsByName('aa');
var value = 0;

for ( ind=0; ind<fileds.length; ind++){
value += Number(fileds[ ind ].value);
}

}
</script>

</body>
</html>
Commented:
<script>
var num1="165.75";
var num2="33";

document.write(num1+num2 + "<hr>")
document.write(num1*1+num2*1 + "<hr>")

//////////////////////////////////////
var num1="165,75";
var num2="33";

document.write(num1+num2 + "<hr>")
document.write(num1.replace(",",".")*1+num2.replace(",",".")*1 + "<hr>")

</script>
Commented:
There is a littele problem with the script i gave you.
for some reason you sometimes get 100.22222222222222222222...
a long number.

this should be fixed by setting the precitage to 2 or 3 as much as you need.

Nushi.
Commented:
parseDecimal() does not exist. Use parseFloat(), that's basically the same as parseInt(), but returns a double and not an integer.
IT ExpertCommented:
As I said:

parseInt if integers
parseFloat(...) if decimals
Author Commented:
I used parseFloat from begining but still don't work
IT ExpertCommented:
Your syntax is not great in the example you post.

Michel
IT ExpertCommented:
HArt:
gives an error so the answer is no
IT ExpertCommented:
DRAT: Too many windows open.. I apologise
Author Commented:
<script>
function Sum(fNum) {
window.document["edit"+fNum].vrednost.value=window.document["cmb"+fNum].select.value;

val=0;
for (var i=1;i<3;i++) {
val+=parseFloat(document["edit"+i].vrednost.value,10)
}
document.suma.vrednost.value=val.toFixed(2);
}
</script>
<table width="100%" border="1">
<tr>
<td>&nbsp;</td>
</tr>
</table>
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="500"> <form name="cmb1" method="post" action="">
<select name="select" size="1" onChange="Sum(1)">
<option value="0"> Monitor</option>
<%
while (!RecordsetMonitor.EOF) {
%>
<option value="<%=(RecordsetMonitor.Fields.Item("CENA3").Value)%>"><%=(RecordsetMonitor.Fields.Item("NARTIKAL").Value)%></option>
<%
RecordsetMonitor.MoveNext();
}
if (RecordsetMonitor.CursorType > 0) {
if (!RecordsetMonitor.BOF) RecordsetMonitor.MoveFirst();
} else {
RecordsetMonitor.Requery();
}
%>
</select>
<style>
SELECT{
width:500px;
}
</style>
</form></td>
<td width="180"> <form name="edit1" method="post" action="">
<input name="vrednost" type="Float" id="vrednost" value="0">
</form></td>
<td width="28%"><form name="suma" method="post" action="">
<div align="center"> Ukupno:
<input name="vrednost" type="Float" value="0">
</div>
</form></td>
</tr>
<tr>

Author Commented:
SORRY mplungjan problem is that the forms for Sum is automaticly filled from combo box - from database and decimal separator is " ," instead " . ". Do you know how to replace this?
IT ExpertCommented:
total=0;
for (var i=1;i<17;i++) {
val = document.suma.elements["edit"+i].vrednost.value
total+=parseFloat(val.replace(',','.'),10)
}
document.suma.vrednost.value=(""+total.toFixed(2)).replace('.',',');

Experts Exchange Solution brought to you by