Okay, why does doing an
average = (x / y) 'where x = 500 and y = 3
average = 167 'should equal 166.6666
how can i make it return the 166 and then strip the decimal?
Printable View
Okay, why does doing an
average = (x / y) 'where x = 500 and y = 3
average = 167 'should equal 166.6666
how can i make it return the 166 and then strip the decimal?
use decimal numbers..
VB Code:
Dim average As Decimal = 0D average = (500D / 3D) MessageBox.Show(Math.Floor(average).ToString)
what is the D for? i cant put average = (xD / yD)
VB Code:
Public Function GetAverage(ByVal x As Integer, ByVal y As Integer) As Integer Dim average As Decimal average = (x / y) Dim daverage As Double = Double.Parse(average) average = (Math.Floor(daverage)) Return average End Function
Use integer division:Note the use of the backslash for integer division rather than the forward slash for floating point division.VB Code:
Dim x As Integer = 500 Dim y As Integer = 3 Dim average As Integer = x \ y
Learned something today... I got a several VB.Net books, and none of them ever mention the different between the / and \ :)Quote:
Originally Posted by jmcilhinney
Quote:
Originally Posted by ooOOJaVaOOoo
D on the end of a number implies it is of type decimal. R is double, I is integer etc...
However Johns method of using the integer division over decimal division will get you there in less code. :wave:
Learning is fun....