Solved

Convert string in to double

Posted on 2003-12-04
17
7,314 Views
Last Modified: 2007-12-19
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>
0
Comment
Question by:elchuma
  • 7
  • 4
  • 4
  • +2
17 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility

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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
 
LVL 10

Expert Comment

by:Nushi
Comment Utility
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
 

Author Comment

by:elchuma
Comment Utility
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
 
LVL 10

Expert Comment

by:Nushi
Comment Utility
thats beacuse its a parseInt
use parseDecimal

0
 
LVL 10

Expert Comment

by:Nushi
Comment Utility
<!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
 
LVL 25

Expert Comment

by:devic
Comment Utility
<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
 
LVL 10

Expert Comment

by:Nushi
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:Christian_Wenz
Comment Utility
parseDecimal() does not exist. Use parseFloat(), that's basically the same as parseInt(), but returns a double and not an integer.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
As I said:

parseInt if integers
parseFloat(...) if decimals
0
 

Author Comment

by:elchuma
Comment Utility
I used parseFloat from begining but still don't work
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Please post form and script.
Your syntax is not great in the example you post.

Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
HArt:
<input type="text" onFocus="this.type='password'">
gives an error so the answer is no
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
DRAT: Too many windows open.. I apologise
0
 

Author Comment

by:elchuma
Comment Utility
<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
 

Author Comment

by:elchuma
Comment Utility
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 35 total points
Comment Utility
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now