Round & FormatNumber 四捨五入的区别
VBScript Round() 四捨五入函数,但是使用 Round() 四捨五入为近偶取整,平时多数是以大于等于 5 的数值进位、小于 5 的数值捨去。FormatNumber() 的值比较符合平常用的值。使用 Round() 近偶取整则于偶数才进位与 FormatNumber() 四捨五入有差异由下表来分析差异点。这种方式可以减少四捨五入误差的累积,但也可能造成一些不符合预期的结果,所产生的误差。
运算的数值 | Round() | FormatNumber() | 差异处 |
---|---|---|---|
0.1 | Round(0.1, 0) = 0 | FormatNumber(0.1, 0) = 0 | - |
0.2 | Round(0.2, 0) = 0 | FormatNumber(0.2, 0) = 0 | - |
0.3 | Round(0.3, 0) = 0 | FormatNumber(0.3, 0) = 0 | - |
0.4 | Round(0.4, 0) = 0 | FormatNumber(0.4, 0) = 0 | - |
0.5 | Round(0.5, 0) = 0 | FormatNumber(0.5, 0) = 1 | 差异处 |
0.6 | Round(0.6, 0) = 1 | FormatNumber(0.6, 0) = 1 | - |
0.7 | Round(0.7, 0) = 1 | FormatNumber(0.7, 0) = 1 | - |
0.8 | Round(0.8, 0) = 1 | FormatNumber(0.8, 0) = 1 | - |
0.9 | Round(0.9, 0) = 1 | FormatNumber(0.9, 0) = 1 | - |
1.0 | Round(1.0, 0) = 1 | FormatNumber(1.0, 0) = 1 | - |
1.1 | Round(1.1, 0) = 1 | FormatNumber(1.1, 0) = 1 | - |
1.2 | Round(1.2, 0) = 1 | FormatNumber(1.2, 0) = 1 | - |
1.3 | Round(1.3, 0) = 1 | FormatNumber(1.3, 0) = 1 | - |
1.4 | Round(1.4, 0) = 1 | FormatNumber(1.4, 0) = 1 | - |
1.5 | Round(1.5, 0) = 2 | FormatNumber(1.5, 0) = 2 | - |
1.6 | Round(1.6, 0) = 2 | FormatNumber(1.6, 0) = 2 | - |
1.7 | Round(1.7, 0) = 2 | FormatNumber(1.7, 0) = 2 | - |
1.8 | Round(1.8, 0) = 2 | FormatNumber(1.8, 0) = 2 | - |
1.9 | Round(1.9, 0) = 2 | FormatNumber(1.9, 0) = 2 | - |
2.0 | Round(2.0, 0) = 2 | FormatNumber(2.0, 0) = 2 | - |
2.1 | Round(2.1, 0) = 2 | FormatNumber(2.1, 0) = 2 | - |
2.2 | Round(2.2, 0) = 2 | FormatNumber(2.2, 0) = 2 | - |
2.3 | Round(2.3, 0) = 2 | FormatNumber(2.3, 0) = 2 | - |
2.4 | Round(2.4, 0) = 2 | FormatNumber(2.4, 0) = 2 | - |
2.5 | Round(2.5, 0) = 2 | FormatNumber(2.5, 0) = 3 | 差异处 |
2.6 | Round(2.6, 0) = 3 | FormatNumber(2.6, 0) = 3 | - |
2.7 | Round(2.7, 0) = 3 | FormatNumber(2.7, 0) = 3 | - |
2.8 | Round(2.8, 0) = 3 | FormatNumber(2.8, 0) = 3 | - |
2.9 | Round(2.9, 0) = 3 | FormatNumber(2.9, 0) = 3 | - |
3.0 | Round(3.0, 0) = 3 | FormatNumber(3.0, 0) = 3 | - |
3.1 | Round(3.1, 0) = 3 | FormatNumber(3.1, 0) = 3 | - |
3.2 | Round(3.2, 0) = 3 | FormatNumber(3.2, 0) = 3 | - |
3.3 | Round(3.3, 0) = 3 | FormatNumber(3.3, 0) = 3 | - |
3.4 | Round(3.4, 0) = 3 | FormatNumber(3.4, 0) = 3 | - |
3.5 | Round(3.5, 0) = 4 | FormatNumber(3.5, 0) = 4 | - |
3.6 | Round(3.6, 0) = 4 | FormatNumber(3.6, 0) = 4 | - |
3.7 | Round(3.7, 0) = 4 | FormatNumber(3.7, 0) = 4 | - |
3.8 | Round(3.8, 0) = 4 | FormatNumber(3.8, 0) = 4 | - |
3.9 | Round(3.9, 0) = 4 | FormatNumber(3.9, 0) = 4 | - |
4.0 | Round(4.0, 0) = 4 | FormatNumber(4.0, 0) = 4 | - |
4.1 | Round(4.1, 0) = 4 | FormatNumber(4.1, 0) = 4 | - |
4.2 | Round(4.2, 0) = 4 | FormatNumber(4.2, 0) = 4 | - |
4.3 | Round(4.3, 0) = 4 | FormatNumber(4.3, 0) = 4 | - |
4.4 | Round(4.4, 0) = 4 | FormatNumber(4.4, 0) = 4 | - |
4.5 | Round(4.5, 0) = 4 | FormatNumber(4.5, 0) = 5 | 差异处 |
4.6 | Round(4.6, 0) = 5 | FormatNumber(4.6, 0) = 5 | - |
4.7 | Round(4.7, 0) = 5 | FormatNumber(4.7, 0) = 5 | - |
4.8 | Round(4.8, 0) = 5 | FormatNumber(4.8, 0) = 5 | - |
4.9 | Round(4.9, 0) = 5 | FormatNumber(4.9, 0) = 5 | - |
5.0 | Round(5.0, 0) = 5 | FormatNumber(5.0, 0) = 5 | - |
5.1 | Round(5.1, 0) = 5 | FormatNumber(5.1, 0) = 5 | - |
5.2 | Round(5.2, 0) = 5 | FormatNumber(5.2, 0) = 5 | - |
5.3 | Round(5.3, 0) = 5 | FormatNumber(5.3, 0) = 5 | - |
5.4 | Round(5.4, 0) = 5 | FormatNumber(5.4, 0) = 5 | - |
5.5 | Round(5.5, 0) = 6 | FormatNumber(5.5, 0) = 6 | - |
5.6 | Round(5.6, 0) = 6 | FormatNumber(5.6, 0) = 6 | - |
5.7 | Round(5.7, 0) = 6 | FormatNumber(5.7, 0) = 6 | - |
5.8 | Round(5.8, 0) = 6 | FormatNumber(5.8, 0) = 6 | - |
5.9 | Round(5.9, 0) = 6 | FormatNumber(5.9, 0) = 6 | - |
6.0 | Round(6.0, 0) = 6 | FormatNumber(6.0, 0) = 6 | - |
6.1 | Round(6.1, 0) = 6 | FormatNumber(6.1, 0) = 6 | - |
6.2 | Round(6.2, 0) = 6 | FormatNumber(6.2, 0) = 6 | - |
6.3 | Round(6.3, 0) = 6 | FormatNumber(6.3, 0) = 6 | - |
6.4 | Round(6.4, 0) = 6 | FormatNumber(6.4, 0) = 6 | - |
6.5 | Round(6.5, 0) = 6 | FormatNumber(6.5, 0) = 7 | 差异处 |
6.6 | Round(6.6, 0) = 7 | FormatNumber(6.6, 0) = 7 | - |
6.7 | Round(6.7, 0) = 7 | FormatNumber(6.7, 0) = 7 | - |
6.8 | Round(6.8, 0) = 7 | FormatNumber(6.8, 0) = 7 | - |
6.9 | Round(6.9, 0) = 7 | FormatNumber(6.9, 0) = 7 | - |
7.0 | Round(7.0, 0) = 7 | FormatNumber(7.0, 0) = 7 | - |
7.1 | Round(7.1, 0) = 7 | FormatNumber(7.1, 0) = 7 | - |
7.2 | Round(7.2, 0) = 7 | FormatNumber(7.2, 0) = 7 | - |
7.3 | Round(7.3, 0) = 7 | FormatNumber(7.3, 0) = 7 | - |
7.4 | Round(7.4, 0) = 7 | FormatNumber(7.4, 0) = 7 | - |
7.5 | Round(7.5, 0) = 8 | FormatNumber(7.5, 0) = 8 | - |
7.6 | Round(7.6, 0) = 8 | FormatNumber(7.6, 0) = 8 | - |
7.7 | Round(7.7, 0) = 8 | FormatNumber(7.7, 0) = 8 | - |
7.8 | Round(7.8, 0) = 8 | FormatNumber(7.8, 0) = 8 | - |
7.9 | Round(7.9, 0) = 8 | FormatNumber(7.9, 0) = 8 | - |
8.0 | Round(8.0, 0) = 8 | FormatNumber(8.0, 0) = 8 | - |
8.1 | Round(8.1, 0) = 8 | FormatNumber(8.1, 0) = 8 | - |
8.2 | Round(8.2, 0) = 8 | FormatNumber(8.2, 0) = 8 | - |
8.3 | Round(8.3, 0) = 8 | FormatNumber(8.3, 0) = 8 | - |
8.4 | Round(8.4, 0) = 8 | FormatNumber(8.4, 0) = 8 | - |
8.5 | Round(8.5, 0) = 8 | FormatNumber(8.5, 0) = 9 | 差异处 |
8.6 | Round(8.6, 0) = 9 | FormatNumber(8.6, 0) = 9 | - |
8.7 | Round(8.7, 0) = 9 | FormatNumber(8.7, 0) = 9 | - |
8.8 | Round(8.8, 0) = 9 | FormatNumber(8.8, 0) = 9 | - |
8.9 | Round(8.9, 0) = 9 | FormatNumber(8.9, 0) = 9 | - |
9.0 | Round(9.0, 0) = 9 | FormatNumber(9.0, 0) = 9 | - |
9.1 | Round(9.1, 0) = 9 | FormatNumber(9.1, 0) = 9 | - |
9.2 | Round(9.2, 0) = 9 | FormatNumber(9.2, 0) = 9 | - |
9.3 | Round(9.3, 0) = 9 | FormatNumber(9.3, 0) = 9 | - |
9.4 | Round(9.4, 0) = 9 | FormatNumber(9.4, 0) = 9 | - |
9.5 | Round(9.5, 0) = 10 | FormatNumber(9.5, 0) = 10 | - |
9.6 | Round(9.6, 0) = 10 | FormatNumber(9.6, 0) = 10 | - |
9.7 | Round(9.7, 0) = 10 | FormatNumber(9.7, 0) = 10 | - |
9.8 | Round(9.8, 0) = 10 | FormatNumber(9.8, 0) = 10 | - |
9.9 | Round(9.9, 0) = 10 | FormatNumber(9.9, 0) = 10 | - |
Round() 及 FormatNumber() Rounding 主要差异处比较
FormatNumber() 函数
FormatNumber(2.5,0)
3
FormatNumber(4.5,0)
5
FormatNumber(6.5,0)
7
FormatNumber(8.5,0)
9
Round() 函数
Round(2.5)
2
Round(4.5)
4
Round(6.5)
6
Round(8.5)
8
Coding 时 ASP 四捨五入函数之使用则依照需求来选择 Round() 及 FormatNumber() 函数。
ASPX IEEEremainder
Math.IEEEremainder(Double, Double) 传回指定数字除以另一个指定数字所得的余数,根据 IEEE 754 二进位浮点数算术标准。使用 Math.IEEEremainder 可以避免舍入误差,并且保持与 IEEE 754 标准的一致性。例如在计算角度或周期性函数时,可以使用 Math.IEEEremainder 来将角度限制在一个范围内,而不会出现负数或超过 360 度的情况。IEEE 754 标准是定义浮点数表示和运算的国际标准。浮点数是用来近似表示实数的数值格式,它由符号位、指数位和有效数位组成。IEEE 754 标准规定了不同的浮点数格式,例如单精度、双精度和扩展精度,以及相关的运算规则,常使用于加法、减法、乘法、除法和取余数。IEEE 754 标准还定义特殊的浮点数值,例如无穷大、非数和捨入模式。IEEE 754 标准的目的是为了提供一种可移植和可靠的浮点数计算方法,并且能够处理各种异常情况。