简体   繁体   English

如何根据小数舍入一个数字? (SQL)

[英]How to round a number based on the decimal? (SQL)

Hello guys my question is this: 大家好,我的问题是这样的:

I have a rule to validate if a number is greater or equal than .3 I will round to the next number and add another one. 我有一条规则可以验证一个数字是否大于或等于.3,我将四舍五入到下一个数字并添加另一个。 Example: 例:

1000.3 = 1001 + 1 = 1002

If the number is less than .3 I will only add one: 如果数字小于.3我将仅添加一个:

1000.2 = 1001

I have been looking for some kind of command in SQL but I had no luck. 我一直在寻找某种SQL命令,但是我没有运气。 Hope you could help me. 希望你能帮助我。

Thanks! 谢谢! Ro A. 罗阿

You can use something like: 您可以使用类似:

SELECT CASE WHEN (Number - FLOOR(Number)) >= .3 
            THEN (CEILING(Number)+1)
            ELSE CEILING(Number)
       END AS NextNumber

I originally wrote that this is ANSI, but it's not. 我最初写道这是ANSI,但事实并非如此。 However SQL Server and MySQL both support the % operator. 但是,SQL Server和MySQL都支持%运算符。

DECLARE @one DECIMAL(10, 1) = 1000.3

 SELECT  CASE WHEN @one % CAST(@one AS INT) >= 0.3 THEN CAST(@one as INT) + 1
    ELSE CAST(@one as INT) + 2 END
SELECT ROUND(15.193,-1) "Round" FROM DUAL;

     Round
----------
        20 

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

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