Get the number of digits after the decimal point of a float (with or without decimal part)

前端 未结 6 1612
走了就别回头了
走了就别回头了 2020-12-18 16:45

I have following list of Amount (float) in my table.

Amount
123
123.1
123.0123
123.789456

How can i get the number of digits a

6条回答
  •  死守一世寂寞
    2020-12-18 17:14

    And one more way:

    SELECT  Amount,
            CASE WHEN deci = 0 THEN 0 ELSE LEN(deci) END AS Result
    FROM (
        SELECT  Amount,
                TRY_CAST(REVERSE(REPLACE(Amount - TRY_CAST(Amount as int),'0.','')) as int) as deci
        FROM (VALUES
        (123),
        (123.1),
        (123.0123),
        (123.789456)
        ) as t (Amount)
    ) as t
    

    Output:

    Amount      Result
    123.000000  0
    123.100000  1
    123.012300  4
    123.789456  6
    

提交回复
热议问题