Javascript Multiplication BUG???

HI

When i run this script

<script language="javascript">

alert(34.97 * 100)
alert(34.98 * 100)
alert(34.99 * 100)
alert(1.98 * 100)
alert(1.99 * 100)

</script>

I get the following results

3497
3497.9999999999995
3499
198
199

Can anyone explain why i get this ?


Cheers

Len
LVL 10
lengreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

archrajanCommented:
see this link http://www.merlyn.demon.co.uk/pas-real.htm#Float
it gives u the details... this is a bug!
0
archrajanCommented:
that link shows that some times using float variables can be tricky
0
rk_radhakrishnaCommented:
Check out http://www.merlyn.demon.co.uk/pas-real.htm#Float. This doesn't give an exact answer to your specific question, but it suggests that floating point numbers can be tricky. Just because something looks to us like it'd be a round number apparently doesn't mean it is in floating point arithmetic.
0
GwynforWebCommented:
this is not a big just the way the floats works in a simple implenatation as in the JS interpreter,  using  val=val.toFixed(3) cleans it up for outputting format

<script language="javascript">
  alert((34.98 * 100).toFixed(2))
</script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lengreenAuthor Commented:
Cheers Guys

Puts my mind at rest... I'm not going insane :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.