SQL Server rounding Error, Giving different values

前端 未结 2 496
夕颜
夕颜 2020-12-11 09:37

I have a Stored Procedure that does a lots of calculation, stores the results in several temporary table. Finally calculating the sum and rounding to two decimal and stores

相关标签:
2条回答
  • 2020-12-11 09:55

    This is because you are using float database type.

    Float should not be used to represent values that require precision, since they are stored as approximations, different manipulations can give you different results.

    In sql server you can use decimal and numeric data types for numerical precision: http://msdn.microsoft.com/en-us/library/ms187746.aspx

    0 讨论(0)
  • 2020-12-11 10:18

    Try this way:

       SELECT ROUND(@test,2) AS Result; --> results 7585.23
       SELECT ROUND(convert(float,7585.225),2) AS Result --> results 7585.23
    

    When you store the value as float it stores the approximate value but not exact value, when you want to store exact value use Decimal, money or small money data type. Here in your example when I converted the numeric value to float, it stored the approximate value of number.

    http://msdn.microsoft.com/en-us/library/ms187912%28v=sql.105%29.aspx

    0 讨论(0)
提交回复
热议问题