查看: 950|回复: 6
|
VB 的 CINT(..) function..
[复制链接]
|
|
为什么我
CINT(3/2) 是 = 2 ???
CINT(5/2) 也会是 = 2 ???
是 bug?? 还是 vb rules??? |
|
|
|
|
|
|
|
发表于 21-5-2006 08:31 AM
|
显示全部楼层
没有记错的话,一般咱们是四舍五入,
不过 CInt 的规则是小数前
单数:四舍五入
双数:五舍六入 |
|
|
|
|
|
|
|
发表于 21-5-2006 10:12 AM
|
显示全部楼层
原帖由 eastken 于 21-5-2006 03:55 AM 发表
为什么我
CINT(3/2) 是 = 2 ???
CINT(5/2) 也会是 = 2 ???
是 bug?? 还是 vb rules???
其实,不只是cint会这样。。。round。。。还有把single或double assign给integer也会这样。。。
气死我了。。。最讨厌vb的这种怪习惯。。。
你再试试以下这几个。。。
Debug.Print Round(1.5)
Debug.Print Round(2.5)
Debug.Print Round(1.54)
Debug.Print Round(2.54)
Debug.Print Round(1.56)
Debug.Print Round(2.56)
得到的结果是:
2
2
2
3
2
3
把round换成CINT也有一样的结果。。。 |
|
|
|
|
|
|
|
发表于 21-5-2006 11:19 PM
|
显示全部楼层
这不是 bug, 也不是 VB rule, integer type 本来就没有小数点. 试试看执行以下的, 要显示出小数点, 就要好好运用 Round 的功能.:
CDbl(3/2)
CDbl(5/2)
Round(1.5, 2)
Round(2.5, 2)
[ 本帖最后由 goatstudio 于 21-5-2006 11:23 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 22-5-2006 09:59 PM
|
显示全部楼层
meemee CInt 其实就是 Round 来的吧
goatstudio大哥,小章鱼想楼主是想问为何小数转整数不是四舍五入吧。 |
|
|
|
|
|
|
|
发表于 22-5-2006 10:50 PM
|
显示全部楼层
CInt 不是 Round, 那是 convert 去 Integer, Integer 没有小数点.
要有小数点的, 可以用 CDbl 或 Round. |
|
|
|
|
|
|
|
发表于 23-5-2006 07:30 AM
|
显示全部楼层
原帖由 sson 于 22-5-2006 21:59 发表
小章鱼想楼主是想问为何小数转整数不是四舍五入吧。
CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.
取自MSDN Library
再来一个网上找到的例子:
Dim LValue As Integer
LValue = CInt(8.45)
In this example, the variable called LValue would now contain the value of 8.
Be careful using CInt. If you were to use the following code:
Dim LValue As Integer
LValue = CInt(8.5)
The variable LValue would still contain the value of 8. Until the fraction is greater than .5, the CInt function will not round the number up.
If the fraction is less than or equal to .5, the result will round down.
If the fraction is greater than .5, the result will round up.
We've found it useful in the past to add 0.0001 to the value before applying the CInt function to simulate the regular rounding process.
For example, CInt(8.50001) will result in a value of 9.
这下子。。。相信大家和楼主应该明白为什么了
[ 本帖最后由 johe07 于 23-5-2006 07:49 AM 编辑 ] |
|
|
|
|
|
|
| |
本周最热论坛帖子
|