Visual Basic 6.0 中的自动类型转换 [英] Automatic type conversion in Visual Basic 6.0

查看:25
本文介绍了Visual Basic 6.0 中的自动类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在 Visual Basic 6.0 中将浮点数转换为整数时,它如何舍入小数部分?我说的是自动类型转换.

When we convert a float to integer in visual basic 6.0, how does it round off the fractional part? I am talkin about the automatic type conversion.

如果我们像这样赋值

Dim i as Integer
i=5.5
msgbox i

它会打印什么?5 或 6 ??

What will it print? 5 or 6 ??

几个月前我得到了5".有一天它开始给我 6 个!知道出了什么问题吗?微软是否发布了一些补丁来修复某些问题?

I was getting "5" a couple of months before. One day it started giving me 6! Any idea whats goin wrong? Did microsoft released some patches to fix something?

更新:5.5 转换为 6 但 8.5 转换为 8 !

Update : 5.5 gets converted to 6 but 8.5 to 8 !

更新 2:添加 CInt 没有区别.CInt(5.5) 给出 6 而 Cint(8.5) 给出 8!!有点奇怪的行为.我应该尝试像 floor(x + 0.49);

Update 2 : Adding CInt makes no difference. CInt(5.5) gives 6 and Cint(8.5) gives 8!! Kinda weired behaviour. I should try something like floor(x + 0.49);

推荐答案

部分内容位于 VB6 帮助:主题 类型转换函数.不幸的是,它是 VB6 文档中没有的主题之一 在 MSDN 网站上,但如果您已经安装了 VB6 的帮助,它将在那里.

Part of this is in the VB6 help: topic Type Conversion Functions. Unfortunately it's one of the topics that's not in the VB6 documentation on the MSDN website, but if you've installed the help with VB6, it will be there.

当小数部分正好是 0.5 时,CIntCLng 总是将其四舍五入到最接近的偶数.例如,0.5 轮到 0,1.5 轮到 2.CIntCLngFixInt 不同> 函数,它截断而不是舍入数字的小数部分.此外,FixInt 始终返回与传入的类型相同的值.

When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in.

隐式类型强制 - 又名 邪恶类型强制 (PDF)"- 从浮点数到整数使用与 CIntCLng 相同的舍入规则.手册中似乎没有记录这种行为.

Implicit type coercion - a.k.a. "evil type coercion (PDF)" - from a floating point number to a whole number uses the same rounding rules as CInt and CLng. This behaviour doesn't seem to be documented anywhere in the manual.

如果你想在小数部分 >= 0.5 时向上取整,否则向下取整,一个简单的方法是

If you want to round up when the fractional part is >= 0.5, and down otherwise, a simple way to do it is

 n = Int(x + 0.5)

我的头顶上,这是我对Mike Spross 的功能的简短版本这是 VB6 Round 函数的替代品.

And off the top of my head, here's my briefer version of Mike Spross's function which is a replacement for the VB6 Round function.

 'Function corrected, now it works. 
Public Function RoundNumber(ByVal value As Currency, Optional PlacesAfterDecimal As Integer = 0) As Currency
  Dim nMultiplier As Long
  nMultiplier = 10 ^ PlacesAfterDecimal
  RoundNumber = Fix(0.5 * Sgn(value) + value * nMultiplier) / CDbl(nMultiplier)
End Function

示例输出:

Debug.Print RoundNumber(1.6)       '2'
Debug.Print RoundNumber(-4.8)      '-5'
Debug.Print RoundNumber(101.7)     '102'
Debug.Print RoundNumber(12.535, 2) '12.54'

这篇关于Visual Basic 6.0 中的自动类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆