1

when i'm trying to sum numbers into one variable from mysql query (the field type is double without format) with php it give me a not normal result.

let say i got few row from query, first row 0.54 and 2nd row 0.2, etc. in the end the result should be 2.433 if i sum all rows manually.

but when i echo the variable shown as this >> 2.433000000000000273558953267638571560382843017578125

where is the rest number come from? and how to prevent it come without use round or format it? or did i miss something?

Edit : I have try to change it to decimal 15,4 but it still sum is as before like this 2.433000000000000273558953267638571560382843017578125 and the wierd thing is the number shown as normally and change after count on php for function.

Ephoenixs
  • 11
  • 3
  • Use `decimal` instead of `float`. – Gordon Linoff Oct 30 '15 at 11:53
  • 1
    Possible duplicate of [Perl/mysql floating point imprecision](http://stackoverflow.com/questions/24398132/perl-mysql-floating-point-imprecision) – Mario Trucco Oct 30 '15 at 11:54
  • So that's why i always get wrong result. Thanks everyone. Then i have to change every double field into decimal. – Ephoenixs Oct 31 '15 at 12:28
  • after i test is the real problem is summary on php FOR function, it give float result not decimal, even after i change the field type. how to force php count it as decimal? – Ephoenixs Nov 02 '15 at 03:25

1 Answers1

1

If you need to make maths calculations on those values, you should consider using DECIMAL instead of DOUBLE or FLOAT in MysQL.

Take a look here : difference between float and decimal data type

Community
  • 1
  • 1
ôkio
  • 1,772
  • 1
  • 15
  • 16
  • after i test is the real problem is summary on php FOR function, it give float result not decimal, even after i change the field type. how to force php count it as decimal? – Ephoenixs Nov 02 '15 at 03:25