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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

having problems with addition!? 9

Status
Not open for further replies.

fayna

Programmer
Jan 14, 2002
35
0
0
ES
Hello,
I never thought someone could have problems adding two numbers, but I have them. Here's some sample code:
var op1 = "119.88"
var op1_1 = "7"
var op2 = "119.88"
var op2_2 = "4"
var res1 = parseFloat(op1) * parseInt(op1_1)
var res2 = parseFloat(op2) * parseInt(op2_2)
var result = res1 + res2
document.write(result)

The result must be 1318.68 but JavaScript returns 1318.67999999999998, where is the error?
Vars res1 and res2 are ok, the problem presents when I try to add them
Thanks
 
I would expect it to be the use of parseFloat(). Try this:
Code:
  var op1 = 119.88;
  var op1_1 = 7;
  var op2 = 119.88;
  var op2_2 = 4;
  var res1 = op1 * op1_1;
  var res2 = op2 * op2_2;
  var result = res1 + res2;
  document.write(result)
I removed the quotes from your variable initializations and got rid of the parse___() functions. Good luck! -gerrygerry
Go To
 
Thanks for your interest gerrygerry, but the problem persits. Please help..
 
Leave your quotes there and try this

var res1 = eval(op1 * op1_1)

It should work
Have Fun...

Sharky99 >:):O>
 
I've tried with eval and the result is the same.
 
Is there any function or command for limitating the number of decimal digits?
 
I've found the answer: document.write(result.toFixed(2))
 
Good...now i feel very rusty thanx hahaha ;-) Have Fun...

Sharky99 >:):O>
 
I've never heard of the toFixed function before. Looks very useful! Fayna gets a star! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hello,

I never heard too about the toFixed function.It's great.
I give fayna a star too!


alexfusion
 
Wow! That's pretty cool! fayna gets another star.
bluebrain.gif
blueuniment.gif
 
Hi guys,
Not meaning no reduce the enthusiasm, just a small comment.

[tt]toFixed[/tt] function is implemented in latest JS 1.5 ver, that means that lots of browsers doesn't "understand" it.
It is recognized by Netscape 6 (see it here:
will not be recognized by Opera (as it supports JS 1.4), not recognized by any Netscape 4. Don't know about IE - I didn't search for it's specs. (This is true for Win platform, but others - who knows...)

So I'll still use (and recommend to you) the "conventional" way of rounding numbers, which is writing a function to do this.

The idea is this: we have good old [tt]Math.round()[/tt] function that rounds to nearest highest integer.
So, in order to round 43.43625 to 2nd number after decimal point, we should do the following:
1) 43.43625 * 100
2) round the result of 1)
3) result of 2) / 100
the result is 43.44

In general, in order to round some number n to m numbers after decimal point, the algorythm is this:
( round( n * 10^m ) ) / 10^m

It is also very good because it's suitable for any programming language (that have basic round feature).
Actually, I have it ready:

function roundIt(somenumber, precision) {

result = ( Math.round (somenumber * Math.pow(10, precision) ) ) / Math.pow(10, precision);

return(result)
}

Works everywhere.
good luck.
 
Very good.
starway gets a star
LOL LOL LOL LOL

Sorry I cant log in and give you a star starway, I might get kicked off again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top