将float转换为varchar时避免Round Up

时间:2014-03-05 04:10:11

标签: sql-server floating-point type-conversion decimal rounding

将float转换为varchar时遇到了问题,而且它总是向上舍入。例如

如果我转换: 0.65它变成了1,但我只想要0 1.55它变成了2,但我只想要1

这是我的代码:

  Declare @Dt1 dateTime Set @Dt1 = '2014-03-05 10:15:53.110'
  Declare @Dt2 dateTime Set @Dt2 = getdate()

  Select str((Cast((@Dt2 - @Dt1) as Float) * 24.0)) as hours1 , 
  str(Cast((@Dt2 - @Dt1) as Float) * 24.0*60.0)%60 as minutes1

转换时可以避免四舍五入吗?

1 个答案:

答案 0 :(得分:2)

您是否尝试过ROUND方法?但是,以下SQL可能符合您的要求:

Select str((Cast((@Dt2 - @Dt1) as Float) * 24.0)) as hours1 , 
  str(ROUND(Cast((@Dt2 - @Dt1) as Float) * 24.0*60.0, 0, 1))%60 as minutes1