|
-
Oct 16th, 2006, 08:45 AM
#1
Thread Starter
Hyperactive Member
Get the average
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?
-
Oct 16th, 2006, 08:51 AM
#2
Re: Get the average
use decimal numbers..
VB Code:
Dim average As Decimal = 0D
average = (500D / 3D)
MessageBox.Show(Math.Floor(average).ToString)
-
Oct 16th, 2006, 08:56 AM
#3
Thread Starter
Hyperactive Member
Re: Get the average
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
-
Oct 16th, 2006, 08:56 AM
#4
Re: Get the average
Use integer division:
VB Code:
Dim x As Integer = 500
Dim y As Integer = 3
Dim average As Integer = x \ y
Note the use of the backslash for integer division rather than the forward slash for floating point division.
-
Oct 16th, 2006, 02:04 PM
#5
Re: Get the average
 Originally Posted by jmcilhinney
Use integer division:
VB Code:
Dim x As Integer = 500
Dim y As Integer = 3
Dim average As Integer = x \ y
Note the use of the backslash for integer division rather than the forward slash for floating point division.
Learned something today... I got a several VB.Net books, and none of them ever mention the different between the / and \
-
Oct 16th, 2006, 02:08 PM
#6
Re: Get the average
 Originally Posted by ooOOJaVaOOoo
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
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.
-
Oct 16th, 2006, 10:25 PM
#7
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|