为什么在SQL Server中四舍五入为从float到varchar [英] Why casting from float to varchar is being rounded in SQL Server

查看:156
本文介绍了为什么在SQL Server中四舍五入为从float到varchar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的sql

  declare @a as float,@b as float 

select @a (@a as VARCHAR(40))AS a_float_to_varchar,
CAST(@b as VARCHAR(40))AS b_float_to_varchar
<= $ 1.353954
select
CAST / code>

结果

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


基于

http://msdn.microsoft.com/en-us/library/ms173773.aspx



float的精度为15位数,所以我不确定为什么数字被转换为varchar时被舍入为解决方案

同样来自您的链接(实际上是第一行):


近似数字数据类型...

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

这就是说,有一个函数 STR()专门用于将 float 转换为字符数据类型。


Following 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

results in

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

based on

http://msdn.microsoft.com/en-us/library/ms173773.aspx

float has 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...

If you want exact precision, don't use 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天全站免登陆