[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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
0
lengreen
Asked:
lengreen
3 Solutions
 
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
 
lengreenAuthor Commented:
Cheers Guys

Puts my mind at rest... I'm not going insane :)
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now