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

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse 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,則其值必須介於 153 之間。If n is specified, it must be a value between 1 and 53. n 的預設值為 53The default value of n is 53.

nn 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) ] 資料類型從 153 的所有 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.

當您想要從 floatreal 轉換成字元資料時,使用 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&#41函式 (Transact-SQL)For more information, see STR (Transact-SQL) and Functions (Transact-SQL).

如果您將使用科學記號標記法的 float 值轉換成 decimalnumeric,就會限制為只有 17 個有效位數的值。Conversion of float values that use scientific notation to decimal or numeric is restricted to values of precision 17 digits only. 任何小於 5E-18 的值都會捨去為 0。Any value < 5E-18 rounds down to 0.

另請參閱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)