为什么在 SQL Server 中从 float 转换为 varchar 被舍入?

下面的SQL,

declare @a as float, @b as float

select @a=1.353954 , @b=1.353956
select 
CAST(@a as VARCHAR(40)) AS a_float_to_varchar ,
CAST(@b as VARCHAR(40)) AS b_float_to_varchar

结果

a_float_to_varchar                       b_float_to_varchar
---------------------------------------- ----------------------------------------
1.35395                                  1.35396

基于 'float' 和 'real' (Transact-SQL).

Float 的精度为 15 位,所以我不确定为什么在转换为 varchar 时会四舍五入.

Float has a precision of 15 digits, so I am not sure why the number is being rounded when converted to varchar.

推荐答案

也来自你的链接(实际上是第一行):

Also from your link (it's actually the first line):

近似数字数据类型...

Approximate-number data types...

如果您想要精确的精度,请不要使用 float.

If you want exact precision, don't use float.

话虽如此,有一个函数STR()用于将 float 转换为字符数据类型.

That being said, there is a function STR() specifically for converting float to a character data type.

相关文章