Page 1 of 1
float field default
Posted: Sun Aug 31, 2008 11:45 am
by m2babaey
Hi
I have float fields in my mysql db ( for price). it shows zeros when there is noting as default. i don't want that zeros. what can i do?
thanks
Re: float field default
Posted: Sun Aug 31, 2008 12:52 pm
by greyhoundcode
If you are storing a price or any other financial value then you should stay clear of floating point types, no matter what the level of precision is, as it is in the nature of floating point calculations that certain 'dead-on integers' have to be approximated, so 0 could be rendered as 0.00000001 or 100 as 99.999999998 and so on (those figures are purely illustrative).
Instead try the decimal type which allows for the precise storage of (particularly monetary) values. The syntax is DECIMAL(S,D) where S is the number of significant places and D is the number of digits after the decimal point.
As an example, if you set a column to DECIMAL(9,2) this gives you nine significant places with two digits after the decimal point, in other words a range of -9,999,999.99 to 9,999,999.99.