Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numbers addition problem?

Status
Not open for further replies.

pichi

Programmer
Nov 12, 2000
156
EC
hi everybody, i have a problem when i make a decimal number addition, the problem is that when i add two decimal number the result is a decimal number with a lot of decimal, when my numbers have two decimal, so what is the problem?
can you help me?
any help would be appreciated
thanks in advance!

Juan Pablo
 
Will you post an example of the script you are running and what the final number looks like?
 
Hi tleish

Here is my code:
var Accum=0; (define var)
Accum=parseFloat(document.forms[0].Suma.value) (if there is a value to add to the adittion result)
Accum +=parseFloat(document.forms[0].Costo.value); (this is the new value to add)
document.forms[0].Suma.value="";
document.forms[0].Suma.value=Accum; (asign the result to the field)

for example if i do the following add:
2.22 + 8.88 it gives me 11.100000000000001!!

hope you can help me!!
Juan Pablo
 
its a js bug with floating point rounding, but if you want you can (for this instance) use Math.round(2.22 + 8.88).

just a work-around for your problem... jared@aauser.com -
 
The problem is that Javascript store floating point numbers as binary, and just as there are certain fractions (one-third for example) that cannot be properly represented in base 10, so are there fractions that cannot be properly represented in base 2. My best solution (for dollars and cents calculations) is to do all calculations with WHOLE numbers (i.e. number of cents), and then use some string parsing to convert the results to dollars and cents before displaying. It's not a perfect soluntion, but it worked for me.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top