FLOAT 和 DOUBLE 类型表示近似数值数据。MySQL 对单精度值使用四个字节,对双精度值使用八个字节。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.
对于 FLOAT,SQL 标准允许在 FLOAT 关键字后用括号可选地指定位精度(但不指定索引范围),即 FLOAT(。MySQL 也支持这种可选精度的指定,但在 p)FLOAT( 中的精度值仅用于确定存储大小。精度从 0 到 23 会产生 4 字节的单精度 p)FLOAT 列。精度从 24 到 53 会产生 8 字节的双精度 DOUBLE 列。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(.
MySQL also supports this optional precision specification, but
the precision value in
p)FLOAT(
is used only to determine storage size. A precision from 0 to 23
results in a 4-byte single-precision p)FLOAT
column. A precision from 24 to 53 results in an 8-byte
double-precision DOUBLE column.
MySQL 允许一种非标准语法:FLOAT( 或 M,D)REAL( 或 M,D)DOUBLE
PRECISION(M,D)。这里,( 意味着数值可以存储最多 M,D)M 位数字,其中 D 位可以是小数点后的数字。例如,一个定义为 FLOAT(7,4) 的列会显示为 -999.9999。MySQL 在存储数值时会进行四舍五入,所以如果你向 FLOAT(7,4) 列插入 999.00009,近似结果是 999.0001。MySQL permits a nonstandard syntax:
FLOAT(
or
M,D)REAL(
or M,D)DOUBLE
PRECISION(.
Here,
M,D)(
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. For example, a column defined as
FLOAT(7,4) is displayed as
-999.9999. MySQL performs rounding when
storing values, so if you insert 999.00009
into a FLOAT(7,4) column, the approximate
result is 999.0001.
FLOAT( 和 M,D)DOUBLE( 是非标准的 MySQL 扩展;并且已被弃用。您应该预期在未来的 MySQL 版本中这些变体的支持将被移除。M,D)FLOAT(and
M,D)DOUBLE(
are nonstandard MySQL extensions; and are deprecated. You should
expect support for these variants to be removed in a future
version of MySQL.M,D)
由于浮点值是近似值,而不是以精确值存储,尝试在比较中将它们当作精确值处理可能会导致问题。它们还可能受到平台或实现的依赖。有关更多信息,请参见 B.3.4.8 节,“浮点值的问题”。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”.
为了最大程度的可移植性,需要存储近似数值数据的代码应使用 FLOAT 或 DOUBLE PRECISION,且不指定精度或数字位数。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.