sql-server-2012

How do I convert a float to varchar without loss of decimal places? SQL Server 2012


SQL Server 2012: converting a float to varchar is removing decimal places.

For example:

select convert(varchar, cast(834.7833 as float))

returns a varchar with 834.783.

How do I convert a float to varchar without loss of decimal places?

This is a similar question to this unanswered question:

How does convert(varchar, float) decide how many decimal places to keep?


Solution

  • Why do you need to do a CAST(... AS FLOAT) at all??

    Try these snippets - they return the full number of decimal points:

    SELECT
        CONVERT(VARCHAR(20), 834.7833)
    
    SELECT
        CAST(834.7833 AS VARCHAR(20))
    

    Both of these output this result:

    834.7833