ROUND(X), ROUND(X,D)
Rounds the argument X to D decimal places. The rounding algorithm depends on the data type of
X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the
value X to become zero.
mysql> SELECT ROUND(-1.23);
-> -1
mysql> SELECT ROUND(-1.58);
-> -2
mysql> SELECT ROUND(1.58);
Mathematical Functions
1839
-> 2
mysql> SELECT ROUND(1.298, 1);
-> 1.3
mysql> SELECT ROUND(1.298, 0);
-> 1
mysql> SELECT ROUND(23.298, -1);
-> 20
The return value has the same type as the first argument (assuming that it is integer, double, or
decimal). This means that for an integer argument, the result is an integer (no decimal places):
mysql> SELECT ROUND(150.000,2), ROUND(150,2);
+------------------+--------------+
| ROUND(150.000,2) | ROUND(150,2) |
+------------------+--------------+
| 150.00 | 150 |
+------------------+--------------+
ROUND() uses the following rules depending on the type of the first argument:
• For exact-value numbers, ROUND() uses the “round half away from zero” or “round toward
nearest” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if
positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A
value with a fractional part less than .5 is rounded down to the next integer if positive or up to the
next integer if negative.
• For approximate-value numbers, the result depends on the C library. On many systems, this
means that ROUND() uses the “round to nearest even” rule: A value with a fractional part exactly
halfway between two integers is rounded to the nearest even integer.
The following example shows how rounding differs for exact and approximate values:
mysql> SELECT ROUND(2.5), ROUND(25E-1);
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3 | 2 |
+------------+--------------+
System.out.println(Math.round(2.5));
System.out.println(Math.round(2.6));
System.out.println(Math.round(-2.5));
System.out.println(Math.round(-2.6));
System.out.println(Math.round(-0.6));
System.out.println(Math.round(-0.5));
ans:
3
3
-2
-3
-1
0
多年来,许多人向我指出,VBScript的Round 函数有点奇怪。看起来应该很简单-您选择最接近故事末尾的整数。但是,比如说1.5?有两个最接近的整数。你会上升还是下降?
该回合功能进入到最接近的整数,并且如果有最接近的两个整数然后将其转到甚至一个。 1.5舍入为2,0.5舍入为0。
为什么?为什么不随便说在这种情况下我们总是四舍五入呢?为什么有时四舍五入而又向上舍入?实际上有一个很好的理由!
此算法称为“ 银行家舍入算法”,因为毫不奇怪,银行家使用该算法。假设数据源提供的数据通常是完全分割的数量(一半美元,一半美分,一半份额,等等),但他们希望提供四舍五入的数量。进一步假设数据使用者将要从四舍五入的数据中得出摘要统计信息,即平均值。
理想情况下,当您要取平均值时,您想要尽可能精确地取平均原始数据。但是在现实世界中,我们经常必须对丢失了某些精度的数据取平均值。在这种情况下,Banker的舍入算法会产生更好的结果,因为它不会使半数量的样本始终偏低或持续偏高。假设平均而言,相等数量的半数量将向下取整,并且误差将被抵消。
如果您不相信我,请尝试一下。生成以0.5结尾的随机数字列表,将其四舍五入并取平均值。您会发现,与“总是四舍五入”平均相比,“银行家四舍五入”为您提供了更接近真实平均的结果。
VBScript中的Round ,CInt 和CLng 函数均使用Banker的Rounding算法。
还有两个其他的VBScript函数可将浮点数转换为整数。的诠释功能给你的第一个整数小于或等于它的输入,并且所述修复功能给你的第一个整数更接近零或等于它的输入。这些函数根本不舍入到最接近的整数,它们只是截断小数部分。
link: https://blogs.msdn.microsoft.com/ericlippert/2003/09/26/bankers-rounding/
Bankers Rounding is an algorithm for rounding quantities to integers, in which numbers which are equidistant from the two nearest integers are rounded to the nearest even integer. Thus, 0.5 rounds down to 0; 1.5 rounds up to 2. A similar algorithm can be constructed for rounding to other sets besides the integers (in particular, sets which a constant interval between adjacent members).
Other decimal fractions round as you would expect--0.4 to 0, 0.6 to 1, 1.4 to 1, 1.6 to 2, etc. Only x.5 numbers get the "special" treatment.
So called because banks supposedly use it for certain computations.
The supposed advantage to bankers rounding is that it is unbiased, and thus produces better results with various operations that involve rounding.
It should be noted that it is unbiased only in the limit. That is, an average of all errors approaches 0.0.
http://blogs.msdn.com/ericlippert/archive/2003/09/26/53107.aspx
https://github.com/WardCunningham/remodeling
link: http://wiki.c2.com/?BankersRounding