Float Conversion To String

  • Hi everybody, i have this piece of code

    declare @units float,@str varchar(20)

    SELECT @STR=''

    select @units=5465465497976.0

    SELECT @STR =@str+@units

    SELECT @STR

    When i execute it, i get the following result:

    5.46547e+012

    IT converts the float in such a way that after 6 digits value is ignored.

    How can i get the string in the original form '5465465497976.0' ?

  • Well, basically, a float is an imprecise datatype, and you can't rely on retrieveing it exactly as it was entered. You will always experience rounding errors with floats.

    If the format you have is consistent (1 decimal), use a decimal instead.

    Here's an example of different behaviours:

    declare @f float

    set @f = 5465465497976.0

    select @f

    -----------------------------------------------------

    5465465497976.0

    select cast(@f as varchar(20))

    --------------------

    5.46547e+012

    select cast(cast(@f as decimal(20,1))as varchar(20))

    --------------------

    5465465497976.0

    /Kenneth

Viewing 2 posts - 1 through 1 (of 1 total)

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