简体   繁体   English

SQL中小于.5的小数如何四舍五入到后面的数?

[英]How to round decimals smaller than .5 to the following number in SQL?

I'm having this situation where a I have a large database with +1000 products.我遇到这种情况,我有一个包含 +1000 种产品的大型数据库。

Some of them have prices like 12.3, 20.7, 55.1 for example.例如,其中一些的价格为 12.3、20.7、55.1。

| Name     | Price          |
| -------- | -------------- |
| Product 1| 12.3           |
| Product 2| 20.7           |
| Product 3| 55.1           |

(and so on)... (等等)...

What I've tried is update prices set price = ROUND (price, 0.1) .我试过的是update prices set price = ROUND (price, 0.1) The output for this will be: output 将是:

| Name     | Price          |
| -------- | -------------- | (after updated)
| Product 1| 12.3           | 12.0
| Product 2| 20.7           | 21.0
| Product 3| 55.1           | 55.0

the prices with decimals <.5 will remain the same, and I'm out of ideas.小数点 <.5 的价格将保持不变,我没有想法。

I'll appreciate any help.我会很感激任何帮助。

Note I need to update all rows,Ii'm trying to learn about CEILING() but only shows how to use it with SELECT , any idea on how to perform an UPDATE CEILING or something?请注意,我需要更新所有行,我正在尝试了解CEILING()但仅显示如何将其与SELECT一起使用,关于如何执行UPDATE CEILING或其他内容的任何想法?

It's not entirely clear what you're asking, but I can tell you the function call as shown makes no sense.不完全清楚你在问什么,但我可以告诉你 function 调用没有意义。

The second argument to the ROUND() function is the number of decimal places, not the size of the value you wish to round to. ROUND() function的第二个参数是小数位数,而不是您希望舍入到的值的大小。 Additionally, the function only accepts integral types for that argument.此外,function 仅接受该参数的整数类型。 Therefore, if you pass the value 0.1 to the function what will happen is the value is first cast to an integer , and the result of casting 0.1 to an integer is 0 .因此,如果您将值0.1传递给 function 将会发生的情况是首先将值转换为 integer ,然后将0.1转换为0的结果是 B.

We see then, that calling ROUND(price, 0.1) is the same as calling ROUND(price, 0) .然后我们看到,调用ROUND(price, 0.1)与调用ROUND(price, 0)相同。

If you want to round to the nearest 0.1 , that's one decimal place and the correct value for the ROUND() function is 1 .如果要四舍五入到最接近的0.1 ,那是小数点后一位, ROUND() function 的正确值为1

ROUND(price, 1)

Compare results here:在此处比较结果:

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=7878c275f0f9ea86f07770e107bc1274 https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=7878c275f0f9ea86f07770e107bc1274

Note the trailing 0's remain, because the fundamental type of the value is unchanged.注意尾随的 0 仍然存在,因为值的基本类型没有改变。 If you also want to remove the trailing 0`s, then you're really moving into the realm of strings, and for that you should wait and the client code, application, or reporting tool handle the conversion.如果您还想删除尾随的 0,那么您实际上是在进入 realm 字符串,为此您应该等待客户端代码、应用程序或报告工具处理转换。

Use UPDATE prices SET PRICE = CEILING(PRICE);使用UPDATE prices SET PRICE = CEILING(PRICE); to get the next whole number for values with decimal >0.5获取小数 > 0.5 的值的下一个整数

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

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