简体   繁体   中英

round a number in SQL

I have a number and I use ROUND() function in SQL :

SELECT ROUND(1.81999,2,1)

I want the result 1.82, not 1.81.
I don't know where my mistake is.

SELECT ROUND(1.81999,2,1) Will truncate the result. so the result is 1.81

use this to perform original rounding SELECT ROUND(1.81999,2).

mysql? just ROUND(...,2) work for me.

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.
a quote from MSDN about this parameter:
When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated .

Just change that 1 at the end to a 0 or ommit it entirely and problem solved

See Section "Round to truncate" of Round(Transact-SQL)

This probably depends on the SQL-Dialect you use.

Use this query

select round(1.81999,2)

Manoj

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