3

我正在尝试将一些转换decimalsvarchar,但它们正在变得四舍五入

有人能告诉我为什么吗?

declare @UpperLeftLatitude DECIMAL,
    @UpperLeftLongitude DECIMAL,
    @BottomRightLatitude DECIMAL,
    @BottomRightLongitude DECIMAL

SET @UpperLeftLatitude = 38.663
SET @UpperLeftLongitude = -122.857
SET @BottomRightLatitude = 37.795
SET @BottomRightLongitude = -121.219


DECLARE @SearchRectangleString VARCHAR(MAX);
SET @SearchRectangleString = 'POLYGON((' + CONVERT(VARCHAR(50), @UpperLeftLatitude) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + ',' 
    + CAST(@BottomRightLatitude AS VARCHAR(50)) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + ',' 
    + CAST(@BottomRightLatitude AS VARCHAR(50)) + ' ' + CAST(@BottomRightLongitude AS VARCHAR(50)) + ',' 
    + CAST(@UpperLeftLatitude AS VARCHAR(50)) + ' ' + CAST(@BottomRightLongitude AS VARCHAR(50)) + ',' 
    + CAST(@UpperLeftLatitude AS VARCHAR(50)) + ' ' + CAST(@UpperLeftLongitude AS VARCHAR(50)) + '))';

    SELECT @SearchRectangleString

-------------------
POLYGON((39 -123,38 -123,38 -121,39 -121,39 -123))

(1 row(s) affected)

注意:是的,我知道我的纬度/经度是错误的。我很快就会换掉它们。

4

2 回答 2

6

这是因为您的小数没有指定长度。他们不存储任何小数位。

尝试以下操作:

 DECLARE @test DECIMAL, @test2 DECIMAL(8,4)
 SET @test = 12.3456
 SET @test2 = 12.3456

 SELECT @test, @test2
于 2010-07-28T07:39:36.043 回答
1

正如 ck 提到的,它变得四舍五入......

当 function 被省略或值为 0(默认)时, numeric_expression 被舍入。当指定 0 以外的值时,numeric_expression 将被截断。

来源:ROUND (T-SQL)

于 2010-07-28T07:46:55.297 回答