Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CInt vs. Math.Round in Visual Basic .NET

Tags:

vb.net

What is the difference between:

    Dim a As Integer = CInt(2.2)

and

    Dim a As Integer = Math.Round(2.2)

?

like image 332
AlwaysLearning Avatar asked Jan 30 '26 02:01

AlwaysLearning


1 Answers

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 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))
like image 105
DontPanic345 Avatar answered Feb 01 '26 18:02

DontPanic345



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!