Visual Basic .NET 中的 CInt 与 Math.Round [英] CInt vs. Math.Round in Visual Basic .NET

查看:26
本文介绍了Visual Basic .NET 中的 CInt 与 Math.Round的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

    Dim a As Integer = CInt(2.2)

    Dim a As Integer = Math.Round(2.2)

?

推荐答案

CInt 返回一个整数,但会将 .5 舍入到最接近的偶数,因此:

CInt returns an integer but will round the .5 to nearest even number so:

2 = CInt(2.5)
4 = CInt(3.5)

两者都是正确的,这可能不是您想要的.

Are both true, which might not be what you want.

Math.Round 可以被告知从零舍入.但是返回一个double,所以我们仍然需要转换它

Math.Round can be told to round away from zero. But returns a double, so we still need to cast it

3 = CInt(Math.Round(2.5, MidpointRounding.AwayFromZero))

这篇关于Visual Basic .NET 中的 CInt 与 Math.Round的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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