Good way to format decimal in SQL Server

We store decimal (9.8) in our database. It can have any number of places after the decimal point (well, no more than 8). I'm upset because I want to display it as human-readable text, as part of a larger line created on the server. I want the number of decimal places to the right of the decimal point to be nonzero, for example:

0.05
0.12345
3.14159265

Things are good

If i do

CAST(d AS varchar(50)) 

I get formatting as:

0.05000000
0.12345000
3.14159265

I get similar output if I drop / convert to float or another type before casting to varchar. I know how to do a fixed number of decimal places, for example:

0.050
0.123
3.142

But that is not what I want.

Yes, I know that I can do this with complex string manipulations (REPLACE, etc.), there should be a good way to do this.

+3
3

( sql), , float .

select cast( cast(0.0501000 as float) as varchar(50) )

0.0501

+2

( 6- 0):

DECLARE @num3 TABLE (i decimal(9, 8))

INSERT  @num3
        SELECT  0.05
        UNION ALL
        SELECT  0.12345
        UNION ALL
        SELECT  3.14159265 
SELECT  i
       ,CASE WHEN PATINDEX('%[1-9]%', REVERSE(i)) < PATINDEX('%.%', REVERSE(i))
             THEN LEFT(i, LEN(i) - PATINDEX('%[1-9]%', REVERSE(i)) + 1)
             ELSE LEFT(i, LEN(i) - PATINDEX('%.%', REVERSE(i)))
        END 'Converted'
FROM    @num3
+2

- , , tbh, , , SQL , , , , . , , , .

0

Source: https://habr.com/ru/post/1745830/