简体   繁体   English

为什么SQL语句SELECT ROUND(45.926,-1)FROM DUAL print 50

[英]Why SQL statement SELECT ROUND(45.926, -1) FROM DUAL print 50

How does dual work? 双重运作如何? I have executed this with many values Select Round(45.926,-1) from dual it prints 50 while Select Round(45.926,-2) from dual prints 0. 我执行了很多值Select Round(45.926,-1) from dual打印50,而Select Round(45.926,-1) from dual打印Select Round(45.926,-2) from dual打印0。

When using ROUND function, the second parameter represents the places after decimal to be taken in result. 当使用ROUND函数时,第二个参数代表小数点后的结果。 So, following are results: 因此,结果如下:

SELECT ROUND(45.926) FROM dual;

Result: 46 because no places after decimal to be considered. 结果:46,因为小数点后无位置可考虑。

SELECT ROUND(45.926, 1) FROM dual;

Result 45.9, consider 1 place after decimal 结果45.9,考虑小数点后1位

SELECT ROUND(45.926, -1) FROM dual;

Result: 50, considering the value to be 4.5926, rounding it to 5 and then printing the result. 结果:50,考虑值为4.5926,将其舍入为5,然后打印结果。

SELECT ROUND(45.926, -2) FROM dual;

Result: 0, considering the value to be 0.45926, rounding it to 0 and then printing the result in original 10x which is 0x100 = 0. 结果:0,考虑值为0.45926,将其四舍五入为0,然后将结果打印为原始10x,即0x100 = 0。

If you'd taken, 如果您采取了,

SELECT ROUND(55.926, -2) FROM dual;

Then result will be 100 because it would be like, rounding 0.55926 to 1 and then 10x which 1x100 = 100. 然后结果将是100,因为它将是这样,将0.55926舍入为1,然后舍入为10x,即1x100 = 100。

Round(n,m) - rounds the number n to m decimal places. Round(n,m) -将数字n舍入到小数位m。

For m = -1, it rounds the number to tenth place making 45.926 to 50 as it is greater than 45 对于m = -1,它将数字四舍五入到第十位,使得45.926为50,因为它大于45

For m = -2, it rounds the number to 100 place. 对于m = -2,它将数字四舍五入到100位。 Since 45.926 is less than 50, it becomes 0. If you try round(51,-2) you should get 100 由于45.926小于50,因此它变为0。如果尝试round(51,-2),则应该得到100

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

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