float field default

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

float field default

Post 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
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: float field default

Post 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.
Post Reply