Jump to content


Photo

A strange db calculation error


  • Please log in to reply
1 reply to this topic

#1 dodgyJim

dodgyJim
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationMelbourne, Australia

Posted 22 March 2006 - 08:32 AM

Hiya,

I have a strange problem. In my db table I have a field called "shipping". It obviously holds a dollar amount.

I am looping through my products and calculating the shipping charges. This is where it gets interesting.

In the db I have an amount like 1.30, when I loop through the query and calculate (shipping * quantity) it screws the amount up.

Shipping = 1.30
Quantity = 2

2 x 1.30 is giving me 2.59999990463
Whatever happened to 2.60?

Any ideas?

cheers.

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 22 March 2006 - 12:28 PM

Floating point arithmetic is always screwy on computers because converting a decimal to binary cleanly isn't possible. Just make sure you always round things to the precision you need. In MySQL ROUND(2.599999, 2) = 2.60




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users