MySQL Forums
Forum List  »  Newbie

FLOAT should not be used for Money
Posted by: Rick James
Date: July 04, 2013 01:35PM

The problem has to do with what FLOAT can and cannot do.

It cannot represent 1.1 exactly.

Also, FLOAT and DOUBLE are representations that have a different amount of precision. When you say
WHERE price = 1.1
and price is FLOAT, you are mixing precisions and/or FLOAT vs DECIMAL.

I have a table with
  `b` decimal(3,2) DEFAULT NULL,
  `f` float DEFAULT NULL,
  `d` double DEFAULT NULL,

INSERT INTO deci (b,f,d) VALUES (1.1, 1.1, 1.1);

SELECT b, f, d, b=f, f=d, b=d, b=1.1, f=1.1, d=1.1 FROM deci
+------+------+------+------+------+------+-------+-------+-------+
| b    | f    | d    | b=f  | f=d  | b=d  | b=1.1 | f=1.1 | d=1.1 |
+------+------+------+------+------+------+-------+-------+-------+
| 1.10 |  1.1 |  1.1 |    0 |    0 |    1 |     1 |     0 |     1 |
+------+------+------+------+------+------+-------+-------+-------+
In the last 6 columns, note that the result of an '=' test is either 0 (meaning FALSE) or 1 (meaning TRUE). The FLOAT version of "1.1" always fails to compare equal.

Bottom line: For Money, use something like DECIMAL(8,2).

Options: ReplyQuote


Subject
Written By
Posted
FLOAT should not be used for Money
July 04, 2013 01:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.