自己写一个: Public Function Round(ByVal x As Double, Optional ByVal dp As Integer = 0) As Double x = x * 10 ^ dp + 0.5 Round = Int(x) / 10 ^ dp End Function
Public Function Round(ByVal x As Double, Optional ByVal dp As Integer = 0) As Double x = x * 10 ^ dp + 0.5 Round = Int(x) / 10 ^ dp End Function
首先,你用的Round()是在哪个语言环境里的。以下是在C++或Pascal或Delphi下的Round()说明: Pascal function Round(X: Extended): Int64; C++ Int64 Round(Extended X); ========================================= Description In Delphi, the Round function rounds a real-type value to an integer-type value. X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker's Rounding". 意思是说,如果X值恰好在两个整数中间,那么函数值偶数那个数。 例如: Round(1.5) = 2 //1和2之间,取偶数2 Round(2.5) = 2 //2和3之间,取偶数2 Round(3.5) = 4 //3和4之间,取偶数4在你的语言环境里,你测试以下值是多少,就应该知道该怎么办了。 Round(1.49) = ? Round(2.49) = ? Round(3.49) = ?Round(1.51) = ? Round(2.51) = ? Round(3.51) = ?
1.25留一位,则1.2=round(1.25)
如果是1.35,则1.4=round(1.35)
奇进偶不进。这样在一大串需要四舍五入的数相加相减相乘时误差小一点。eg:1.25+1.35+1.45+1.55+1.15+1.65=8.4=round(1.25)+round(1.35)+round(……
ROUND函数符合国家和国际标准,该函数是正确的。原先的四舍五入只是一个简化。
参考:http://blog.sina.com.cn/s/blog_4cbb97be010008o9.html
x = x * 10 ^ dp + 0.5
Round = Int(x) / 10 ^ dp
End Function
x = x * 10 ^ dp + 0.5
Round = Int(x) / 10 ^ dp
End Function
Round(9 / 2)
(9 / 2) :(int 9 / int 2)等于4, Round(4)= 4
9/2=4.5, round(4.5)=4,
11/2=5.5, round(5.5)=6
参考:http://blog.sina.com.cn/s /blog_4cbb97be010008o9.html
这是数据类型问题。9/2是整数相除取商,结果是4,然后round()当然也是4了。
要改变这种情况,需要把至少其中之一操作数变成浮点数,如9.0/2或9/2.0或9.0/2.0等等。
Pascal
function Round(X: Extended): Int64;
C++
Int64 Round(Extended X);
=========================================
Description
In Delphi, the Round function rounds a real-type value to an integer-type value. X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker's Rounding". 意思是说,如果X值恰好在两个整数中间,那么函数值偶数那个数。
例如:
Round(1.5) = 2 //1和2之间,取偶数2
Round(2.5) = 2 //2和3之间,取偶数2
Round(3.5) = 4 //3和4之间,取偶数4在你的语言环境里,你测试以下值是多少,就应该知道该怎么办了。
Round(1.49) = ?
Round(2.49) = ?
Round(3.49) = ?Round(1.51) = ?
Round(2.51) = ?
Round(3.51) = ?
参考:http://blog.sina.com.cn/s/blog_4cbb97be010008o9.html