为什么在 SQL Server 中从 float 转换为 varchar 被舍入? [英] Why is casting from float to varchar being rounded in SQL Server?

查看:28
本文介绍了为什么在 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.

这篇关于为什么在 SQL Server 中从 float 转换为 varchar 被舍入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆