Round & FormatNumber 四捨五入的区别

VBScript Round() 四捨五入函数,但是使用 Round() 四捨五入为近偶取整,平时多数是以大于等于 5 的数值进位、小于 5 的数值捨去。FormatNumber() 的值比较符合平常用的值。使用 Round() 近偶取整则于偶数才进位与 FormatNumber() 四捨五入有差异由下表来分析差异点。这种方式可以减少四捨五入误差的累积,但也可能造成一些不符合预期的结果,所产生的误差。

运算的数值Round()FormatNumber()差异处
0.1Round(0.1, 0) = 0FormatNumber(0.1, 0) = 0-
0.2Round(0.2, 0) = 0FormatNumber(0.2, 0) = 0-
0.3Round(0.3, 0) = 0FormatNumber(0.3, 0) = 0-
0.4Round(0.4, 0) = 0FormatNumber(0.4, 0) = 0-
0.5Round(0.5, 0) = 0FormatNumber(0.5, 0) = 1差异处
0.6Round(0.6, 0) = 1FormatNumber(0.6, 0) = 1-
0.7Round(0.7, 0) = 1FormatNumber(0.7, 0) = 1-
0.8Round(0.8, 0) = 1FormatNumber(0.8, 0) = 1-
0.9Round(0.9, 0) = 1FormatNumber(0.9, 0) = 1-
1.0Round(1.0, 0) = 1FormatNumber(1.0, 0) = 1-
1.1Round(1.1, 0) = 1FormatNumber(1.1, 0) = 1-
1.2Round(1.2, 0) = 1FormatNumber(1.2, 0) = 1-
1.3Round(1.3, 0) = 1FormatNumber(1.3, 0) = 1-
1.4Round(1.4, 0) = 1FormatNumber(1.4, 0) = 1-
1.5Round(1.5, 0) = 2FormatNumber(1.5, 0) = 2-
1.6Round(1.6, 0) = 2FormatNumber(1.6, 0) = 2-
1.7Round(1.7, 0) = 2FormatNumber(1.7, 0) = 2-
1.8Round(1.8, 0) = 2FormatNumber(1.8, 0) = 2-
1.9Round(1.9, 0) = 2FormatNumber(1.9, 0) = 2-
2.0Round(2.0, 0) = 2FormatNumber(2.0, 0) = 2-
2.1Round(2.1, 0) = 2FormatNumber(2.1, 0) = 2-
2.2Round(2.2, 0) = 2FormatNumber(2.2, 0) = 2-
2.3Round(2.3, 0) = 2FormatNumber(2.3, 0) = 2-
2.4Round(2.4, 0) = 2FormatNumber(2.4, 0) = 2-
2.5Round(2.5, 0) = 2FormatNumber(2.5, 0) = 3差异处
2.6Round(2.6, 0) = 3FormatNumber(2.6, 0) = 3-
2.7Round(2.7, 0) = 3FormatNumber(2.7, 0) = 3-
2.8Round(2.8, 0) = 3FormatNumber(2.8, 0) = 3-
2.9Round(2.9, 0) = 3FormatNumber(2.9, 0) = 3-
3.0Round(3.0, 0) = 3FormatNumber(3.0, 0) = 3-
3.1Round(3.1, 0) = 3FormatNumber(3.1, 0) = 3-
3.2Round(3.2, 0) = 3FormatNumber(3.2, 0) = 3-
3.3Round(3.3, 0) = 3FormatNumber(3.3, 0) = 3-
3.4Round(3.4, 0) = 3FormatNumber(3.4, 0) = 3-
3.5Round(3.5, 0) = 4FormatNumber(3.5, 0) = 4-
3.6Round(3.6, 0) = 4FormatNumber(3.6, 0) = 4-
3.7Round(3.7, 0) = 4FormatNumber(3.7, 0) = 4-
3.8Round(3.8, 0) = 4FormatNumber(3.8, 0) = 4-
3.9Round(3.9, 0) = 4FormatNumber(3.9, 0) = 4-
4.0Round(4.0, 0) = 4FormatNumber(4.0, 0) = 4-
4.1Round(4.1, 0) = 4FormatNumber(4.1, 0) = 4-
4.2Round(4.2, 0) = 4FormatNumber(4.2, 0) = 4-
4.3Round(4.3, 0) = 4FormatNumber(4.3, 0) = 4-
4.4Round(4.4, 0) = 4FormatNumber(4.4, 0) = 4-
4.5Round(4.5, 0) = 4FormatNumber(4.5, 0) = 5差异处
4.6Round(4.6, 0) = 5FormatNumber(4.6, 0) = 5-
4.7Round(4.7, 0) = 5FormatNumber(4.7, 0) = 5-
4.8Round(4.8, 0) = 5FormatNumber(4.8, 0) = 5-
4.9Round(4.9, 0) = 5FormatNumber(4.9, 0) = 5-
5.0Round(5.0, 0) = 5FormatNumber(5.0, 0) = 5-
5.1Round(5.1, 0) = 5FormatNumber(5.1, 0) = 5-
5.2Round(5.2, 0) = 5FormatNumber(5.2, 0) = 5-
5.3Round(5.3, 0) = 5FormatNumber(5.3, 0) = 5-
5.4Round(5.4, 0) = 5FormatNumber(5.4, 0) = 5-
5.5Round(5.5, 0) = 6FormatNumber(5.5, 0) = 6-
5.6Round(5.6, 0) = 6FormatNumber(5.6, 0) = 6-
5.7Round(5.7, 0) = 6FormatNumber(5.7, 0) = 6-
5.8Round(5.8, 0) = 6FormatNumber(5.8, 0) = 6-
5.9Round(5.9, 0) = 6FormatNumber(5.9, 0) = 6-
6.0Round(6.0, 0) = 6FormatNumber(6.0, 0) = 6-
6.1Round(6.1, 0) = 6FormatNumber(6.1, 0) = 6-
6.2Round(6.2, 0) = 6FormatNumber(6.2, 0) = 6-
6.3Round(6.3, 0) = 6FormatNumber(6.3, 0) = 6-
6.4Round(6.4, 0) = 6FormatNumber(6.4, 0) = 6-
6.5Round(6.5, 0) = 6FormatNumber(6.5, 0) = 7差异处
6.6Round(6.6, 0) = 7FormatNumber(6.6, 0) = 7-
6.7Round(6.7, 0) = 7FormatNumber(6.7, 0) = 7-
6.8Round(6.8, 0) = 7FormatNumber(6.8, 0) = 7-
6.9Round(6.9, 0) = 7FormatNumber(6.9, 0) = 7-
7.0Round(7.0, 0) = 7FormatNumber(7.0, 0) = 7-
7.1Round(7.1, 0) = 7FormatNumber(7.1, 0) = 7-
7.2Round(7.2, 0) = 7FormatNumber(7.2, 0) = 7-
7.3Round(7.3, 0) = 7FormatNumber(7.3, 0) = 7-
7.4Round(7.4, 0) = 7FormatNumber(7.4, 0) = 7-
7.5Round(7.5, 0) = 8FormatNumber(7.5, 0) = 8-
7.6Round(7.6, 0) = 8FormatNumber(7.6, 0) = 8-
7.7Round(7.7, 0) = 8FormatNumber(7.7, 0) = 8-
7.8Round(7.8, 0) = 8FormatNumber(7.8, 0) = 8-
7.9Round(7.9, 0) = 8FormatNumber(7.9, 0) = 8-
8.0Round(8.0, 0) = 8FormatNumber(8.0, 0) = 8-
8.1Round(8.1, 0) = 8FormatNumber(8.1, 0) = 8-
8.2Round(8.2, 0) = 8FormatNumber(8.2, 0) = 8-
8.3Round(8.3, 0) = 8FormatNumber(8.3, 0) = 8-
8.4Round(8.4, 0) = 8FormatNumber(8.4, 0) = 8-
8.5Round(8.5, 0) = 8FormatNumber(8.5, 0) = 9差异处
8.6Round(8.6, 0) = 9FormatNumber(8.6, 0) = 9-
8.7Round(8.7, 0) = 9FormatNumber(8.7, 0) = 9-
8.8Round(8.8, 0) = 9FormatNumber(8.8, 0) = 9-
8.9Round(8.9, 0) = 9FormatNumber(8.9, 0) = 9-
9.0Round(9.0, 0) = 9FormatNumber(9.0, 0) = 9-
9.1Round(9.1, 0) = 9FormatNumber(9.1, 0) = 9-
9.2Round(9.2, 0) = 9FormatNumber(9.2, 0) = 9-
9.3Round(9.3, 0) = 9FormatNumber(9.3, 0) = 9-
9.4Round(9.4, 0) = 9FormatNumber(9.4, 0) = 9-
9.5Round(9.5, 0) = 10FormatNumber(9.5, 0) = 10-
9.6Round(9.6, 0) = 10FormatNumber(9.6, 0) = 10-
9.7Round(9.7, 0) = 10FormatNumber(9.7, 0) = 10-
9.8Round(9.8, 0) = 10FormatNumber(9.8, 0) = 10-
9.9Round(9.9, 0) = 10FormatNumber(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

CodingASP 四捨五入函数之使用则依照需求来选择 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 标准的目的是为了提供一种可移植和可靠的浮点数计算方法,并且能够处理各种异常情况。