yujx>drop table dd; yujx>create table dd (a double); yujx>insert into dd values(956.745),(231.34243252),(321.43534),(5464.446); yujx>select a,round(a,2) from dd; +--------------+------------+ | a | round(a,2) | +--------------+------------+ | 956.745 | 956.74 | #可以看到并不是我们期望的956.75 | 231.34243252 | 231.34 | | 321.43534 | 321.44 | | 5464.446 | 5464.45 | +--------------+------------+ 4 rows in set (0.00 sec)
The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, seeSection B.5.5.8, “Problems with Floating-Point Values
“Problems with Floating-Point Values” B.5.4.8 Problems with Floating-Point Values
Floating-point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an SQL statement may not be the same as the value represented internally. Attempts to treat floating-point values as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. The FLOAT and DOUBLE data types are subject to these issues. For DECIMALcolumns, MySQL performs operations with a precision of 65 decimal digits, which should solve most common inaccuracy problems.