convert float to string

  • What is the best way to convert a float to a string value with comma separated thousands and no decimals?
     
    This is what I've come up with - is there anything shorter?
     

    substring(convert(varchar(20), cast(projectcost as money), 1),

    1, len(convert(varchar(20), cast(projectcost as money), 1)) - 3)

  • This was a challenge...

    Couldn't find many ways to do it better :-).

    But this is shorter ;-), and will round the float value correctly:

    declare @projectcost float

    set @projectcost = 6842332198.523549

    -- shorter version with correct rounding

    select substring(convert(varchar(20),cast(str(@projectcost) as money),1),1,1+4*(len(convert(bigint,@projectcost))-1)/3)

    -- marginally longer version

    select substring(convert(varchar(20),cast(@projectcost as money),1),1,len(convert(varchar(20),cast(@projectcost as money),1))-3)

    /rockmoose


    You must unlearn what You have learnt

  • That should be Client side Responsibility.

    Just my opinion

     

     


    * Noel

  • And a very good opinion noeld, agree.

    Don't format until the data is presented to the user.

    /rockmoose


    You must unlearn what You have learnt

  • I'm getting this as xml and transforming using xslt. If someone can show me how to do the formatting in xslt that would be cool.

  • whew - only 5 mins to open this page! sites a bit slow for me at the mo

    you can use the format-number function - something like

    <xsl:value-of select="format-number(mynumber, '#,###')" />

    maybe the round function as well

    zvon.org has a really good xslt reference

    http//www.zvon.org/xxl/XSLTreference/Output/index.html

    jt

     

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply