float转化为varchar的精度问题

TLFLY 2003-10-16 10:22:50
使用convert(varchar, float)将float型转化为varchar型,原始数据如下:

730.049987792969
438.029998779297
0
0
11893.01953125

转化后的数据如下:
730.05
438.03
0
0
11893

为什么11893.01953125转化后小数位都没有了呢?
请问在转化时怎么控制转化的精度?
...全文
235 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TLFLY 2003-10-16
  • 打赏
  • 举报
回复
楼上的,在使用你的语句确实可以得到正确结果,可是从表中取出来的时候就错了!
aierong 2003-10-16
  • 打赏
  • 举报
回复
select convert(varchar(20) ,i)
from (
select 730.049987792969 i
union all
select 438.029998779297 i
union all
select 0 i
union all
select 0 i
union all
select 11893.01953125 i) b


zjcxc 2003-10-16
  • 打赏
  • 举报
回复
float直接转换成字符串有限制
先转换成decimal再转换成字符串就行了.

结果:

--------------------------------------
730.049987792969
zjcxc 2003-10-16
  • 打赏
  • 举报
回复
/*
下表显示了从 float 或 real 转换为字符数据时的 style 值。

值 输出
0(默认值) 最大为 6 位数。根据需要使用科学记数法。
1 始终为 8 位值。始终使用科学记数法。
2 始终为 16 位值。始终使用科学记数法。
*/

--这样做就行了
declare @a float
set @a=730.049987792969
select convert(varchar(38),cast(@a as decimal(38,12)))


txlicenhe 2003-10-16
  • 打赏
  • 举报
回复
select convert(varchar(20),11893.01953125)
--------------------
11893.01953125

(所影响的行数为 1 行)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧