Decimals and formatting

  • I've searched through the forums but have not found anything specifically related to my topic.

    If you take the following SQL statement and run it the result is .480

    SELECT CONVERT(DECIMAL(10, 3), 0.4799608993157)

    I want the result to be .479. Is there a way to achieve .479 as a result?

    Thanks,

    Kyle

  • select convert(decimal(10,3),convert(int, (0.4799608993157 * 1000))) / 1000


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

  • Here's another way that might be faster as it's not doing the CONVERTs

    SELECT floor(0.4799608993157 * 1000) / 1000

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Another poster supplied the following script.

    Sorry cant find the thread to give credit.

     

     

    CREATE FUNCTION RemoveDecimalsWithoutRounding

    ( @InputValue Float

    , @Decimals Int

    )

    -- Get the rounded value

    DECLARE @ReturnValue Float;

    SET @ReturnValue = Round(@InputValue, @Decimals);

    -- If the value is too high remove the smalles value allowed by the specified number of decimal places

    IF @ReturnValue > @InputValue

     SET @ReturnValue = @ReturnValue - (1 /  Power(Convert(Float, 10), Convert(Float, @Decimals)));

    PRINT @ReturnValue;

    Modify as you wish for in datatype.

  • SELECT ROUND(0.4799608993157, 3, 1)

  • Thanks everyone! I knew I would get some good solutions here.

    Kyle

  • SELECT  CONVERT(DECIMAL(10, 3),ROUND(0.4799608993157, 3, 1))

     

    This will give result as .479

  • Why do the CONVERT when the ROUND works without it?

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • SELECT  ROUND(0.4799608993157, 3, 1) will return result as

    .4790000000000

     

    SELECT  CONVERT(DECIMAL(10, 3),ROUND(0.4799608993157, 3, 1)) will return result as

    .479

     

    Thanks,

    Murthy

  • To Murthy: You are right, but what is the difference between .4790000000000 and .479 for SQL Server?

    SELECT CONVERT(DECIMAL(10, 3),ROUND(0.4799608993157, 3, 1)) / 1 -- .479000

    It is the client duty to represent these numbers in correct way for the end user.

  • Will this code work in T-SQL or will I have to modify it

    When I run it it I get the following error

    Msg 156, Level 15, State 1, Procedure RemoveDecimalsWithoutRounding, Line 7

    Incorrect syntax near the keyword 'DECLARE'.

    Thanks

  • Most likely a small typo. Can you post the code you are using and the exact error message? That'll help us figure this out quicker for you.

  • The function was posted above by Ray.

    CREATE FUNCTION RemoveDecimalsWithoutRounding ( @InputValue Float, @Decimals Int)

    -- Get the rounded value

    DECLARE @ReturnValue Float;

    SET @ReturnValue = Round(@InputValue, @Decimals);

    -- If the value is too high remove the smalles value allowed by the specified number of decimal places

    IF @ReturnValue > @InputValue

    SET @ReturnValue = @ReturnValue - (1 / Power(Convert(Float, 10), Convert(Float, @Decimals)));

    PRINT @ReturnValue;

    What I can see is this:

    - a function needs to start with

    CREATE FUNCTION RemoveDecimalsWithoutRounding

    ( @InputValue Float, @Decimals Int)

    RETURNS FLOAT

    AS

    BEGIN

    /*body of the function here*/

    RETURN @ReturnValue

    END

    Also, better remove all the semicolons (";") - they don't belong into a SQL function.

    I didn't check the function itself, just what was apparent at first glance.

  • Oh, thank goodness... it's not "Bankers Rounding" 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 14 posts - 1 through 13 (of 13 total)

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