如何计算浮点列中小数点右侧的位数?

发布于 2024-12-28 01:11:21 字数 854 浏览 0 评论 0原文

我有一个带有纬度和经度的表,它们存储为浮点。如何计算“纬度”列中小数点右侧的位数?数据看起来像这样:

    DECLARE @MyData TABLE (ID, Latitude float, Longitude float)
    INSERT @MyData

    SELECT 'A', 33.11, -55.2235 UNION ALL
    SELECT 'B', 33.6407760431,-87.0002760543 UNION ALL
    SELECT 'C', 42.2997,-70.9081; 

我希望在

    ID    |   LatitudeNumberOfDigits
     A    |    2
     B    |   10
     C    |    4

尝试将其转换为文本并使用 .作为分隔符,但 CONVERT 没有按预期工作。它四舍五入到四位有效数字

    SELECT ID, Latitude, Longitude, 
    CONVERT(varchar(max),[Latitude]) AS LatText 
    FROM @MyData

给了我

     ID Latitude    Longitude        LatText
     A  33.11           -55.2235         33.11
     B  33.6407760431   -87.0002760543   33.6408
     C  42.2997         -70.9081         42.2997

谢谢!

I have table with Latitudes and Longitudes that are stored as floating points. How would I count the number of digits to the right of the decimal in the Latitude column? The data would look something like this:

    DECLARE @MyData TABLE (ID, Latitude float, Longitude float)
    INSERT @MyData

    SELECT 'A', 33.11, -55.2235 UNION ALL
    SELECT 'B', 33.6407760431,-87.0002760543 UNION ALL
    SELECT 'C', 42.2997,-70.9081; 

and I would want this in

    ID    |   LatitudeNumberOfDigits
     A    |    2
     B    |   10
     C    |    4

I was trying to convert it to text and split it up using a . as a delimiter but the CONVERT did not work as anticipated. It rounded to four significant digits

    SELECT ID, Latitude, Longitude, 
    CONVERT(varchar(max),[Latitude]) AS LatText 
    FROM @MyData

Gave me

     ID Latitude    Longitude        LatText
     A  33.11           -55.2235         33.11
     B  33.6407760431   -87.0002760543   33.6408
     C  42.2997         -70.9081         42.2997

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国产ˉ祖宗 2025-01-04 01:11:21

试试这个(假设你的浮点数最多有 10 位小数,否则调整 STR 中的值)。

WITH MyData (ID, Latitude, Longitude)
AS
(
    SELECT 'A', CAST(33.11 AS FLOAT), CAST(-55.2235 AS FLOAT) UNION ALL
    SELECT 'B', 33.6407760431,-87.0002760543 UNION ALL
    SELECT 'C', 42.2997,-70.9081 
)
SELECT ID, Latitude, Longitude, 
    LEN(CAST(REVERSE(SUBSTRING(STR([Latitude], 13, 11), CHARINDEX('.', STR([Latitude], 13, 11)) + 1, 20)) AS INT)) AS LatText  
FROM MyData

然而,正如其他人所指出的,FLOAT 可能会给您带来一些麻烦。
例如考虑这个:

SELECT STR(33.11, 20,17) -- result: 33.1099999999999990

Try this (it assumes that your floats will have at most 10 decimal places, otherwise adjust the values in STR).

WITH MyData (ID, Latitude, Longitude)
AS
(
    SELECT 'A', CAST(33.11 AS FLOAT), CAST(-55.2235 AS FLOAT) UNION ALL
    SELECT 'B', 33.6407760431,-87.0002760543 UNION ALL
    SELECT 'C', 42.2997,-70.9081 
)
SELECT ID, Latitude, Longitude, 
    LEN(CAST(REVERSE(SUBSTRING(STR([Latitude], 13, 11), CHARINDEX('.', STR([Latitude], 13, 11)) + 1, 20)) AS INT)) AS LatText  
FROM MyData

As others have note, however, FLOATs are likely to give you some headaches.
Consider this for instance:

SELECT STR(33.11, 20,17) -- result: 33.1099999999999990
如此安好 2025-01-04 01:11:21

你不知道。小数位数由 SQL 中浮点数的大小决定。您看到的数量取决于浮动格式的方式。

You don't. The number of decimal places is determined by the size of a float in SQL. How many you SEE is determined by the way the float is formatted.

烟酒忠诚 2025-01-04 01:11:21

使用 DECIMAL 而不是 FLOAT:

DECLARE @a FLOAT = 33.6407760431
SELECT CAST(@a AS VARCHAR(100))

DECLARE @b DECIMAL(22,10) = 33.6407760431
SELECT CAST(@b AS VARCHAR(100))

返回:

33.6408

33.6407760431

Use DECIMAL not FLOAT:

DECLARE @a FLOAT = 33.6407760431
SELECT CAST(@a AS VARCHAR(100))

DECLARE @b DECIMAL(22,10) = 33.6407760431
SELECT CAST(@b AS VARCHAR(100))

Returns:

33.6408

33.6407760431
心清如水 2025-01-04 01:11:21

转换为数字(最大精度,最大比例)。然后用 case 语句进行迭代。

declare @value as numeric(19,5) = 123.12647

select 
CASE WHEN @value = round(@value,0) then 0
     WHEN @value = round(@value,1) then 1
     WHEN @value = round(@value,2) then 2
     WHEN @value = round(@value,3) then 3
     WHEN @value = round(@value,4) then 4
     else 5 end

Cast as numeric(maxprecision, maxscale). Then iterate with a case statement.

declare @value as numeric(19,5) = 123.12647

select 
CASE WHEN @value = round(@value,0) then 0
     WHEN @value = round(@value,1) then 1
     WHEN @value = round(@value,2) then 2
     WHEN @value = round(@value,3) then 3
     WHEN @value = round(@value,4) then 4
     else 5 end
無心 2025-01-04 01:11:21

这可能不是有效的 tsql,但它会很接近:

SELECT LENGTH(CONVERT(varchar(255), Latitude - CAST (Latitude as int))) AS decimal_place_count 

This may not be valid tsql, but it will be close:

SELECT LENGTH(CONVERT(varchar(255), Latitude - CAST (Latitude as int))) AS decimal_place_count 
趴在窗边数星星i 2025-01-04 01:11:21

使用 STR 函数而不是 CONVERTCAST。它允许您更好地控制小数位数和字符串的总长度。请参阅STR (Transact-SQL)

LEN(RTRIM(REPLACE(STR(Latitude,15,10),'0',' '))) - 5

Use the STR function instead of CONVERT or CAST. It allows you more control on the number of decimals and the total length of the string. See STR (Transact-SQL)

LEN(RTRIM(REPLACE(STR(Latitude,15,10),'0',' '))) - 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文