在下面的查询中
declare @a float(23)
declare @b float(23)
declare @c float(53)
set @a = 123456789012.1234
set @b = 1234567.12345678
set @c = @a * @b
select @c
select LTRIM(STR((@c),32,12))
declare @x decimal(16,4)
declare @y decimal(16,8)
declare @z decimal (32,12)
set @x = 123456789012.1234
set @y = 1234567.12345678
set @z = @x * @y
select @z
我得到的答案是
1.52415693411713E+17
152415693411713020.000000000000
152415692881907790.143935926652
从上面的答案来看,第三个答案是正确的。这就是 float 据类型被称为近似数值数据类型的原因吗
或者我做错了什么根本性的事情。
顺便说一句,这是由于我在遗留系统中遇到的一个问题,其中我必须使用 float 作为存储数据类型,同时在计算时不应该丢失精度。
请提出替代方案或解释。
最佳答案
float 精确到15 significant figures仅(在 SQL Server 中)。
1.52415693411713 E+17
证明了这一点,其中 1.52415693411713
(15 位数字)与您得到的一样准确。 152415693411713
后的最后 020...
STR
是 make up 是浮点分辨率
为保持精度,请勿使用 float 。就这么简单。如果你想进行计算,则 CAST 为十进制,但如果你 CAST 回 float ,则限制为 15 位
参见 "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
关于sql-server - SQL Server Float 数据类型计算 vs decimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8414561/