Len function on Float in SQLServer gives wrong length

前端 未结 3 403
小鲜肉
小鲜肉 2020-12-22 03:44

I am using the below query in SQL Server.

declare @dt float
set @dt = 1079938.05
select @dt AS Val,Convert(nvarchar(20),@dt) AS NVal, len(@dt) AS Len
         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 04:22

    First of all: Don't use approximate data types when not forced to. A FLOAT is just an approximation, e.g. a simple value like 0.123 may be stored as 0.1230000000001 for instance. Use a precise type such as DECIMAL instead.

    When converting a number to a string, you should usually specify a format as in format(@dt, '#,###,##0.00'). You don't do so, so it's up to the system what format to use. It uses a scientific notation 1.07994e+006 translating to 1.079940 x 10^6, which is approximately your number.

提交回复
热议问题