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>
elchumaAsked:
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.

Michel PlungjanIT 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
0
Michel PlungjanIT 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)
0
NushiCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

elchumaAuthor 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?  
0
NushiCommented:
thats beacuse its a parseInt
use parseDecimal

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

<html>
<head>
      <title>Untitled</title>
</head>

<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);
            }

      alert( value );      
      }
</script>

</body>
</html>
0
devicCommented:
<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>
0
NushiCommented:
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.
0
Christian_WenzCommented:
parseDecimal() does not exist. Use parseFloat(), that's basically the same as parseInt(), but returns a double and not an integer.
0
Michel PlungjanIT ExpertCommented:
As I said:

parseInt if integers
parseFloat(...) if decimals
0
elchumaAuthor Commented:
I used parseFloat from begining but still don't work
0
Michel PlungjanIT ExpertCommented:
Please post form and script.
Your syntax is not great in the example you post.

Michel
0
Michel PlungjanIT ExpertCommented:
HArt:
<input type="text" onFocus="this.type='password'">
gives an error so the answer is no
0
Michel PlungjanIT ExpertCommented:
DRAT: Too many windows open.. I apologise
0
elchumaAuthor 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>

Anyone please
0
elchumaAuthor 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?
0
Michel PlungjanIT 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('.',',');
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.