Remove trailing zeros in decimal value with changing length

前端 未结 15 2928
醉话见心
醉话见心 2020-12-05 04:36

I have a procedure I am doing that displays odds but the client wants only significant digits to be shown. So, 1.50 would show as \'1.5\' and 1.00 would show as \'1\'.

15条回答
  •  一个人的身影
    2020-12-05 05:31

    SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
    FROM yourtable
    WHERE yourfield LIKE '%.%'
    

    or

    SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
    FROM yourtable
    WHERE instr(yourfield,'.') != 0
    

    work ok but require a "where" clause.

    I think the best solution is probably:

    SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM ROUND(yourfield,3))) 
    FROM yourtable
    

    as it doesn't require a "where" clause, doesn't require any special code, and also lets you set the maximum precision of the number upfront.

提交回复
热议问题