简体   繁体   English

SQL 服务器圆形 Function 问题

[英]SQL Server Round Function Issue

I'm using SSMS 2014, I got an issue on rounding up the column ( FLOAT ) in my SampleTable .我正在使用 SSMS 2014,在我的SampleTable中舍入列( FLOAT )时遇到问题。 If you'll notice on the below rounding the 7.725 should be 7.73 but the output is 7.72 which is wrong.如果您注意到下面的四舍五入,7.725 应该是 7.73,但 output 是 7.72,这是错误的。 Hope you can help me fix this issue.希望你能帮我解决这个问题。

CODE代码

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

OUTPUT 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.问题是 7.725 不能完全表示为浮点数。

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.换句话说,浮点值略小于 7.725,由于它比 7.73 更接近 7.72,因此结果为 7.72。

On the other hand 7.025 CAN be exactly represented, and this would round to 7.03.另一方面,可以精确表示 7.025,这将四舍五入为 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 ).如果这在您的应用程序中很重要,您可以考虑改用小数类型,它允许精确表示任何十进制数(直到指定的精度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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