Re: Help with MySql float

From: Date: Sun, 17 Feb 2008 23:12:03 +0000
Subject: Re: Help with MySql float
References: 1 2 3 4  Groups: php.db 
Request: Send a blank email to php-db+get-44617@lists.php.net to get a copy of this message
Daniel Brown wrote:
On Feb 17, 2008 10:59 AM, Velen <velen@biz-mu.com> wrote:
Hi Guys, When inserting a value like 123567.8956 in my table it is rounding it to 2 decimal place. The field type is set as float. Can anyone tell me why it's not taking all the decimals? and How to insert the number with all the decimals?
    That's a MySQL question, not a PHP question, but my guess is that
the column is set as FLOAT(x,2) (where x is any real number). Try changing that to FLOAT(10,4). The first number is everything to the left of the decimal, while the second is the number of places to count after the decimal.
Also note that a float type is not guaranteed to come back the same as it goes in (and this will also affect maths operations) - so if you need 4 decimal places, then use decimal or numeric as the data type. http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001. The DECIMAL and NUMERIC data types are used to store exact numeric data values. ..... These types are used to store values for which it is important to preserve exact precision, for example with monetary data. This is the same in any db, it's not a mysql specific behaviour. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.db (#44617) next »