float 和 real (Transact-SQL)float and real (Transact-SQL)

适用对象:是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

用于表示浮点数值数据的大致数值数据类型。Approximate-number data types for use with floating point numeric data. 浮点数据为近似值;因此,并非数据类型范围内的所有值都能精确地表示。Floating point data is approximate; therefore, not all values in the data type range can be represented exactly. real 的 ISO 同义词为 float(24) 。The ISO synonym for real is float(24).

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

float [ (n) ] 其中 n 为用于存储 float 数值尾数的位数(以科学记数法表示),因此可以确定精度和存储大小 。float [ (n) ] Where n is the number of bits that are used to store the mantissa of the float number in scientific notation and, therefore, dictates the precision and storage size. 如果指定了 n,则它必须是介于 1 和 53 之间的某个值 。If n is specified, it must be a value between 1 and 53. n 的默认值为 53 。The default value of n is 53.

n 值 n value 精度Precision 存储大小Storage size
1-241-24 7 位数7 digits 4 个字节4 bytes
25-5325-53 15 位数15 digits 8 字节8 bytes

备注

SQL ServerSQL Server 将 n 视为下列两个可能值之一 。treats n as one of two possible values. 如果 1<=n<=24,将 n 视为 24 。If 1<=n<=24, n is treated as 24. 如果 25<=n<=53,将 n 视为 53 。If 25<=n<=53, n is treated as 53.

SQL ServerSQL Server float[(n)] 数据类型从 1 到 53 之间的所有 n 值均符合 ISO 标准 。The SQL ServerSQL Server float[(n)] data type complies with the ISO standard for all values of n from 1 through 53. double precision 的同义词是 float(53) 。The synonym for double precision is float(53).

RemarksRemarks

数据类型Data type 范围Range 存储Storage
floatfloat -1.79E + 308 至 -2.23E - 308、0 以及 2.23E - 308 至 1.79E + 308- 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 取决于 n 的值 Depends on the value of n
realreal -3.40E + 38 至 -1.18E - 38、0 以及 1.18E - 38 至 3.40E + 38- 3.40E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38 4 个字节4 Bytes

转换 float 和 real 数据Converting float and real data

如果将 float 值转换为任一整数类型,这些值将被截断 。Values of float are truncated when they are converted to any integer type.

若要将 float 或 real 转换为字符数据,使用 STR 字符串函数通常比使用 CAST( ) 更有用 。When you want to convert from float or real to character data, using the STR string function is usually more useful than CAST( ). 这是因为 STR 能够对格式进行更严格的控制。This is because STR enables more control over formatting. 有关详细信息,请参阅 STR (Transact-SQL)函数 (Transact-SQL)For more information, see STR (Transact-SQL) and Functions (Transact-SQL).

在进行 SQL Server 2016 (13.x)SQL Server 2016 (13.x) 之前,浮点值到 decimal 或 numeric 的转换仅限于精度为 17 位数字的值 。Prior to SQL Server 2016 (13.x)SQL Server 2016 (13.x), conversion of float values to decimal or numeric is restricted to values of precision 17 digits only. 小于 5E-18 的任何浮点 值(使用 5E-18 的科学计数法或 0.0000000000000000050000000000000005 十进制表示法设置时)都舍入为 0。Any float value less than 5E-18 (when set using either the scientific notation of 5E-18 or the decimal notation of 0.0000000000000000050000000000000005) rounds down to 0. SQL Server 2016 (13.x)SQL Server 2016 (13.x) 起,不再具有该限制。This is no longer a restriction as of SQL Server 2016 (13.x)SQL Server 2016 (13.x).

另请参阅See also

ALTER TABLE (Transact-SQL)ALTER TABLE (Transact-SQL)
CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)
CREATE TABLE (Transact-SQL)CREATE TABLE (Transact-SQL)
数据类型转换(数据库引擎)Data Type Conversion (Database Engine)
数据类型 (Transact-SQL)Data Types (Transact-SQL)
DECLARE @local_variable (Transact-SQL)DECLARE @local_variable (Transact-SQL)
SET @local_variable (Transact-SQL)SET @local_variable (Transact-SQL)