0

well, I've a javascript code which uses two arrays:

  1. items [id,name, price]
    prices are float from mysql db.

  2. representation of an order in html form which i iterate using jquery

now I compute the total price
I run over the second table and retrieves proper price from the first.
but I get the answer+0.000000001.
which i use tofixed to cut.
but still I'm puzzled if this is a known behavior.

shevski
  • 1,012
  • 1
  • 10
  • 32
  • 1
    How about an actual example? There are **tons** of "known behaviors" when using floats. http://stackoverflow.com/questions/985601 http://stackoverflow.com/questions/3556789 and many more – Matt Ball Jan 28 '11 at 15:44
  • 1
    It's so totally known that you should delete your question – Šime Vidas Jan 28 '11 at 15:48

2 Answers2

1

yes. it's known behavior, and the good example

0.1 + 0.2 = 0.30000000000000004

Igor Milla
  • 2,767
  • 4
  • 36
  • 44
0

Could you update your DB query to multiply the price by 100 and so return it in (whatever your smallest unit of current is... pence, cents, etc)? That way you're dealing with integers and can just format your output when you want to display it.

Nick
  • 2,418
  • 16
  • 20