sql - calculate with decimal problem

nononame2021 261 Reputation points
2021-12-15T11:03:22.097+00:00

I need to perform the calculations and want the output with 2 decimal

how can I take 2 decimal as money datatype without rounding by sql?

219.085 take 219.08

but now I save as money data type but it return 258.08 instead of 219.09?

25*7.7634 +25 =?

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Developer technologies Transact-SQL
SQL Server Other
{count} votes

5 answers

Sort by: Most helpful
  1. nononame2021 261 Reputation points
    2021-12-15T12:39:47.963+00:00

    when u calculate by sql and convert to float then convert to money, it become 219.09

    how to return 219.08


  2. Olaf Helper 47,436 Reputation points
    2021-12-15T13:03:40.32+00:00

    Still can't understand/reproduce your issue:

    select cast(cast(25.0 * 7.7634 + 25.0 as float) as money)  
    

    157913--temp.png

    And when you round to 2 decimals, then 219.09 is the right result.


  3. Viorel 122.6K Reputation points
    2021-12-15T15:24:27.917+00:00

    To truncate the value, try the ROUND function with three arguments like in this example:

    select round( $219.0850, 2, 1)


  4. ZoeHui-MSFT 41,491 Reputation points
    2021-12-16T07:19:44.733+00:00

    Hi @nononame2021 ,

    Also try with below.

    select CAST(round(val - 0.005, 2)  as numeric(18, 2))  
    

    About round function in TSQL, you may refer round-transact-sql

    Note that you have post several issues on Q&A, if the resolution is helpful, please mark it as answer so other user with similar problem could see this easier.

    This behavior could also encourage our experts to offer more help.

    Regards,

    Zoe


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  5. LiHong-MSFT 10,056 Reputation points
    2021-12-16T07:53:05.803+00:00

    Hi,@nononame2021
    Welcome to Microsoft T-SQL Q&A Forum!
    Here are some methods for your reference:

    DECLARE @v decimal(10,5)  
    SET @v=25*7.7634+25  
    --test1  
    SELECT CONVERT(MONEY,ROUND(@v,2,1) )  
      
    --test2  
    SELECT CONVERT(MONEY,LEFT(@v,CHARINDEX('.',@v)+2))  
      
    --test3  
    SELECT CONVERT(MONEY,FLOOR(@v*100)/100)  
    

    By the way,you can search the function in the official documentation.The documents explains very well.

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.