Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer division rounding in VB.NET

When two variables are declared as integer type and you perform

14/4, you get 4, but when you use integer division, 14\4, you get 3.

I thought when you use integer division it rounds to the closest even number. So 14\4 = 3.5 (4 is the closest even number) should be 4 instead, right?

like image 407
user2472706 Avatar asked Jun 27 '13 06:06

user2472706


People also ask

How does integer division round?

If the divisor and dividend have the same sign then the result is zero or positive. If the divisor and dividend have opposite signs then the result is zero or negative. If the division is inexact then the quotient is rounded up.

How do you code integer division in Visual Basic?

Integer division is carried out using the \ Operator (Visual Basic).

How do you divide without rounding?

First, set up the long division. Next, ignore the decimal point and divide as you would divide whole numbers. Then, place the decimal point in the quotient directly above the decimal point in the dividend. The answer is 6.9.


1 Answers

In VB.NET, the / operator is defined to return a floating-point result. It converts the variables to double before performing the division.

This is not the case in the integer division \ where the division is performed without the remainder if the quotient is a decimal (decimals are ignored). For example if the quotient is 3.x, then x is ignored

like image 172
fareed Avatar answered Oct 02 '22 14:10

fareed