0

My database structure for a currency table has one row for the value of the currency

I am trying to set it at 1.2000000 but after I do it changes to 1.20000005

This is causing my some grief with some other parts of my code that checks for decimals.

the structure is set to: float(13,8)

Sackling
  • 1,704
  • 4
  • 33
  • 67

1 Answers1

3

Change the type in database from float to decimal if You need to hold the value of currency

Please check better explanation why not to use floats Use Float or Decimal for Accounting Application Dollar Amount?

Community
  • 1
  • 1
rafwlaz
  • 484
  • 2
  • 15
  • I thought of that but it was already set to float from when the software was originally developed so I didnt want to change too many things. I changed the amount of decimals to 4 from 8 though and that seems to have fixed it without causing any issue. – Sackling Jan 05 '16 at 19:17
  • @Sackling in this example yes, but when You try to insert another value the problem will still exists, so changing to decimal is best way I think, you no need to do anytching more just change type – rafwlaz Jan 05 '16 at 19:18
  • I add that problem is not a database, it's a PHP what makes float numbers "griefed". Changing column to decimal(8,4) in sql will suppress this weirdness. see [link](http://stackoverflow.com/questions/3148937/compare-floats-in-php) – Reloecc Jan 05 '16 at 19:19
  • 1
    It's IEEE754 what makes float numbers "griefed" - https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems – Mark Baker Jan 05 '16 at 19:29