简体   繁体   中英

SQL Server Round Function Issue

I'm using SSMS 2014, I got an issue on rounding up the column ( FLOAT ) in my SampleTable . If you'll notice on the below rounding the 7.725 should be 7.73 but the output is 7.72 which is wrong. Hope you can help me fix this issue.

CODE

SELECT date, ROUND(timeelapsed, 2) AS RoundedValue, timeelapsed 
FROM SampleTable

OUTPUT

date        RoundedValue    timeelapsed
----------------------------------------
2020-02-03  2.78            2.781944445
2020-02-05  0.43            0.433333333
2020-02-06  8.67            8.670833334
2020-02-07  5.78            5.783055556
2020-02-08  2.43            2.431111111
2020-02-09  7.72            7.725
2020-02-10  4               3.996388889

The problem is that 7.725 cannot be represented exactly as a floating point number.

In other words the float value is slightly smaller than 7.725, and since this is closer to 7.72 than 7.73 the result is 7.72.

On the other hand 7.025 CAN be exactly represented, and this would round to 7.03.

If this matters in your application, you could consider using the decimal type instead, which allows an exact representation of any decimal number ( up to the precision specified ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM