您的位置:首页 > 其它

float,double precision,real, decimal,numeric 用法探究

2015-10-26 11:47 197 查看
在TSQL中,Float,real和Double是浮点型,存在精度缺失,是近似数值,近似数字数据类型并不存储为多数数字指定的精确值,它们只储存这些值的最近似值。Decimal和Numeric是精确数值,不存在精度损失。当数据值必须严格按指定存储时,可以使用 decimal 或numeric数据类型来存储带小数的数字。

第一类,近似数值,存在精度损失

Syntax

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. If n is specified, it must be a value between 1 and 53. The default value of n is 53.

n value

Precision

Storage size

1-24

7 digits

4 bytes

25-53

15 digits

8 bytes

Note:SQL Server treats n as one of two possible values. If 1<=n<=24, n is treated as 24. If 25<=n<=53, n is treated as 53.

The SQL Server float[(n)] data type complies with the ISO standard for all values of n from 1 through 53.

The synonym for double precision is float(53).

The ISO synonym for real is float(24).

Note: double precision 组合作为一个DataType

In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.

示例1,由于在TSQL中,小数点会自动转换为numeric类型,1.0自动转换为numeric(2, 1)

--float(24)
declare @f_low float(24)
declare @r real

select @f_low=cast(1.0 as float(53))/3,@r=cast(1.0 as float(53))/3
select 1.0/3 as f,@f_low as f_low,@r as r

--float(53)
declare @f float
declare @dp double PRECISION
declare @f_high float(53)

select @f=cast(1.0 as float(53))/3,@dp=cast(1.0 as float(53))/3,@f_high=cast(1.0 as float(53))/3
select @f as f,@dp as d,@f_high as f_high




在 WHERE 子句搜索条件(特别是 = 和 <> 运算符)中,应避免使用 float 列或 real 列。float 列和 real 列最好只限于 > 比较或 < 比较。

第二类,精确数值,不存在精度损失

Numeric data types that have fixed precision and scale.

decimal [ (p[ ,s] )] and numeric[ (p[ ,s] )]
Fixed precision and scale numbers. When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1. Numeric is functionally equivalent to decimal.

p (precision)
The maximum total number of decimal digits that will be stored, both to the left and to the right of the decimal point. The precision must be a value from 1 through the maximum precision of 38. The default precision is 18.

s (scale)
The number of decimal digits that will be stored to the right of the decimal point. This number is substracted from p to determine the maximum number of digits to the left of the decimal point. Scale must be a value from 0 through p. Scale can be specified only if precision is specified. The default scale is 0; therefore, 0 <= s <= p. Maximum storage sizes vary, based on the precision.

Precision

Storage bytes

1 - 9

5

10-19

9

20-28

13

29-38

17

Note:p 和 s 必须遵守规则:0 <= s <= p <= 38

numeric 和 decimal 数据类型的默认最大精度为 38。在 Transact-SQL 中,numeric 的功能等同于 decimal 数据类型。

decimal 数据类型最多可以存储 38 个数字,所有这些数字均可位于小数点后面。decimal 数据类型存储精确的数字表示形式,存储值没有近似值。

定义 decimal 列、变量和参数的两种属性为:

p 指定精度或对象能够支持的数字个数。

s 指定可以放在小数点右边的小数位数或数字个数。

p 和 s 必须遵守规则:0 <= s <= p <= 38。

numeric 和 decimal 数据类型的默认最大精度为 38。在 Transact-SQL 中,numeric 的功能等同于 decimal 数据类型。

当数据值必须严格按指定存储时,可以使用 decimal 数据类型来存储带小数的数字。

示例2,

DECLARE @dec decimal(38,37)
declare @num numeric(38,37)

select @dec=cast(1.0 as decimal(38,37))/3,@num=cast(1.0 as NUMERIC(38,37))/3
select @dec,@num




Note:

In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.

By default, SQL Server uses rounding when converting a number to a decimal or numeric value with a lower precision and scale. However, if the SET ARITHABORT option is ON, SQL Server raises an error when overflow occurs. Loss of only precision and scale is not sufficient to raise an error.

在 Transact-SQL 语句中,带有小数点的常量自动转换为 numeric 数据值,且必然使用最小的精度和小数位数。例如,常量 12.345 被转换为 numeric 值,其精度为 5,小数位为 3。

默认情况下,在将数字转换为较低精度和小数位数的 decimalnumeric 值时,SQL Server 使用舍入法。然而,如果 SET ARITHABORT 选项为 ON,当发生溢出时,SQL Server 会出现错误。若仅损失精度和小数位数,则不会产生错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: