简体   繁体   English

在SQL中四舍五入一个数字

[英]round a number in SQL

I have a number and I use ROUND() function in SQL : 我有一个数字,并且在SQL中使用ROUND()函数:

SELECT ROUND(1.81999,2,1)

I want the result 1.82, not 1.81. 我想要的结果是1.82,而不是1.81。
I don't know where my mistake is. 我不知道我的错误在哪里。

SELECT ROUND(1.81999,2,1) Will truncate the result. SELECT ROUND(1.81999,2,1)将截断结果。 so the result is 1.81 所以结果是1.81

use this to perform original rounding SELECT ROUND(1.81999,2). 使用它执行原始舍入SELECT ROUND(1.81999,2)。

mysql? MySQL的? just ROUND(...,2) work for me. 仅ROUND(...,2)为我工作。

mysql> SELECT ROUND(1.81999,2);
+------------------+
| ROUND(1.81999,2) |
+------------------+
|             1.82 |
+------------------+
1 row in set (0.00 sec)

mysql>

The answer is simple. 答案很简单。 you are specifying 1 in the Function command (3rd parameter of the ROUND() command. 您在Function命令(ROUND()命令的第3个参数)中指定1。
a quote from MSDN about this parameter: 来自MSDN的关于此参数的报价:
When function is omitted or has a value of 0 (default), numeric_expression is rounded. 如果省略函数或值为0(默认值),则将数字表达式四舍五入。 When a value other than 0 is specified, numeric_expression is truncated . 当指定非0的值时,numeric_expression被截断

Just change that 1 at the end to a 0 or ommit it entirely and problem solved 只需将1的末尾更改为0或完全省略即可解决问题

See Section "Round to truncate" of Round(Transact-SQL) 请参见Round(Transact-SQL)的 “舍入为舍入”部分

This probably depends on the SQL-Dialect you use. 这可能取决于您使用的SQL方言。

Use this query 使用此查询

select round(1.81999,2)

Manoj 马诺吉

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

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