Results 1 to 7 of 7

Thread: Get the average

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    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?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get the average

    use decimal numbers..

    VB Code:
    1. Dim average As Decimal = 0D
    2.         average = (500D / 3D)
    3.         MessageBox.Show(Math.Floor(average).ToString)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    Re: Get the average

    what is the D for? i cant put average = (xD / yD)

    VB Code:
    1. Public Function GetAverage(ByVal x As Integer, ByVal y As Integer) As Integer
    2.         Dim average As Decimal
    3.         average = (x / y)
    4.  
    5.         Dim daverage As Double = Double.Parse(average)
    6.  
    7.         average = (Math.Floor(daverage))
    8.         Return average
    9.     End Function

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the average

    Use integer division:
    VB Code:
    1. Dim x As Integer = 500
    2. Dim y As Integer = 3
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Get the average

    Quote Originally Posted by jmcilhinney
    Use integer division:
    VB Code:
    1. Dim x As Integer = 500
    2. Dim y As Integer = 3
    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 \

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get the average

    Quote Originally Posted by ooOOJaVaOOoo
    what is the D for? i cant put average = (xD / yD)

    VB Code:
    1. Public Function GetAverage(ByVal x As Integer, ByVal y As Integer) As Integer
    2.         Dim average As Decimal
    3.         average = (x / y)
    4.  
    5.         Dim daverage As Double = Double.Parse(average)
    6.  
    7.         average = (Math.Floor(daverage))
    8.         Return average
    9.     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.

  7. #7
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Get the average

    Learning is fun....

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width