codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Calculation error in Javascript  akoju.shiva at 09:10 on Thursday, June 21, 2007
 

Hi,

I've written a small calculation in javascript.
In below example, it consists of 3 textfields and 1 button.
On click of calculate button, it multiplies of textfield1 and textfield2 and displays on textfield3.
But it doesn't show proper multiplication.

For example:
1548 * 17.01 = 26331.48
But it shows as 26331.480000000003
similarly,
1441 * 19.03 = 26331.48
But it shows as 27422.230000000003
It is adding another 10 decimals...

I can use round off method by using toFixed, but i can't change for each and every page of my project.

Can anyone help me regarding the same


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<script language="JavaScript" type="text/javascript">

function toMul()
{
document.getElementById("text3").value = document.getElementById("text1").value * document.getElementById("text2").value;

}

</Script>
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" NAME="MUL1" >
<P>Enter Numbers  <INPUT id=text1
style="LEFT: 113px; TOP: 16px" name=text1><INPUT id=text2 name=text2></P>
<P><INPUT id=button1 type=button value=Calculate name=Calculate onClick="toMul()"></P>
<P>Output <INPUT id=text3 name=text3></P>
</FORM>
</BODY>
</HTML>








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Passing parameters to a perl program from a HTML form
•  Re: calling javascript function in asp page
•  Calculation error in Javascript
•  help try to update parameters in sql server with Stored Procedure in ASP
•  help try to update parameters in sql server with Stored Procedure in ASP
•  help try to update parameters in sql server with Stored Procedure in ASP
•  Re: Can anyone please help me, html table to access.
•  Get strings between tags on web pages
•  Providing time parameters to user when searching


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007