Truncate real/float to two digits

  • I have a real/float datatype that I'd like to display to two digits after the decimal point only. I've tried casting/converting, get an 'arithmetic overflow' error. This seems simple, but googling comes up with lots of stuff that isn't specific to my question (I doubt I'm asking the right question).

    257.09130859375 should equal 257.09

    0.37548828125 should equal 0.37

    0.21923828125 should equal 0.21

    The digits preceding the decimal point could range from 1 to 6, so I can't just use a fixed field size.

    ----------------------------------------------------------------------------
    Sacramento SQL Server users group - http://sac.sqlpass.org
    Follow me on Twitter - @SQLDCH
    ----------------------------------------------------------------------------

    Yeah, well...The Dude abides.
  • How about

    cast(round(float_num,2) as numeric(20,2))


    And then again, I might be wrong ...
    David Webb

  • Yup, was a dumb question. Thanks David for the 'round' function.

    ----------------------------------------------------------------------------
    Sacramento SQL Server users group - http://sac.sqlpass.org
    Follow me on Twitter - @SQLDCH
    ----------------------------------------------------------------------------

    Yeah, well...The Dude abides.
  • If you really want to truncate rather than round, i.e. if you want 0.21 rather than 0.22 given an input of 0.21923828125, then you would need to explicitly truncate using the 3rd (optional) argument of the ROUND function:

    DECLARE @v-2 float

    SELECT @v-2 = 0.21923828125

    SELECT ROUND(@v, 2, 1)

    -- or --

    SELECT CAST(ROUND(@v, 2, 1) AS numeric(20, 2))

Viewing 4 posts - 1 through 3 (of 3 total)

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