The FLOAT
and DOUBLE
types represent approximate numeric data values. FLOAT
和DOUBLE
类型表示近似的数字数据值。MySQL uses four bytes for single-precision values and eight bytes for double-precision values.MySQL使用4个字节表示单精度值,8个字节表示双精度值。
For 对于FLOAT
, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT
in parentheses; ; that is, FLOAT(
. p
)FLOAT
,SQL标准允许在关键字FLOAT
之后的括号中指定一个可选的精度(但不是指数的范围);也就是说,FLOAT(p)
。MySQL also supports this optional precision specification, but the precision value in MySQL也支持这个可选的精度规范,但是FLOAT(
is used only to determine storage size. p
)FLOAT(p)
中的精度值仅用于确定存储大小。A precision from 0 to 23 results in a 4-byte single-precision 从0到23的精度将产生一个4字节的单精度FLOAT
column. FLOAT
列。A precision from 24 to 53 results in an 8-byte double-precision 从24到53的精度产生一个8字节的双精度DOUBLE
column.DOUBLE
列。
MySQL permits a nonstandard syntax: MySQL允许非标准的语法:FLOAT(
or M
,D
)REAL(
or M
,D
)DOUBLE PRECISION(
. M
,D
)FLOAT(M,D)
或REAL(M,)
或DOUBLE PRECISION(M,D)
。Here, 此处,(
means than values can be stored with up to M
,D
)M
digits in total, of which D
digits may be after the decimal point. (M,D)
表示可存储的数值最多为M
位,其中D
位可能在小数点之后。For example, a column defined as 例如,定义为FLOAT(7,4)
is displayed as -999.9999
. FLOAT(7,4)
的列显示为-999.9999
。MySQL performs rounding when storing values, so if you insert MySQL在存储值时执行舍入,因此如果将999.00009
into a FLOAT(7,4)
column, the approximate result is 999.0001
.999.00009
插入FLOAT(7,4)
列,则近似结果为999.0001
。
As of MySQL 8.0.17, the nonstandard 从MySQL8.0.17开始,非标准的FLOAT(
and M
,D
)DOUBLE(
syntax is deprecated and you should expect support for it to be removed in a future version of MySQL.M
,D
)FLOAT(M,D)
和DOUBLE(M,D)
语法就被弃用了,您应该希望在将来的MySQL版本中删除对它的支持。
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, see Section B.3.4.8, “Problems with Floating-Point Values”有关详细信息,请参阅第B.3.4.8节,“浮点值问题”。
For maximum portability, code requiring storage of approximate numeric data values should use 为了最大限度地提高可移植性,需要存储近似数字数据值的代码应使用FLOAT
or DOUBLE PRECISION
with no specification of precision or number of digits.FLOAT
或DOUBLE PRECISION
,不指定精度或位数。