Solved

Convert string in to double

Posted on 2003-12-04
17
7,320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
  • +2
17 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9873077

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
ID: 9873079
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
ID: 9873195
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:elchuma
ID: 9873288
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
ID: 9873297
thats beacuse its a parseInt
use parseDecimal

0
 
LVL 10

Expert Comment

by:Nushi
ID: 9873315
<!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
ID: 9873316
<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
ID: 9873326
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
 
LVL 5

Expert Comment

by:Christian_Wenz
ID: 9873358
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
ID: 9873368
As I said:

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

Author Comment

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

Expert Comment

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

Michel
0
 
LVL 75

Expert Comment

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

Expert Comment

by:Michel Plungjan
ID: 9873426
DRAT: Too many windows open.. I apologise
0
 

Author Comment

by:elchuma
ID: 9873436
<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
ID: 9873465
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
ID: 9873501
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 32
Tools for analysis of code 4 44
links not working 2 25
testing a web application for ipad on windows pc 3 34
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

740 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