Results 1 to 8 of 8

Thread: Rounding Up and Down Conditionally

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    Rounding Up and Down Conditionally

    I have a need to do conditional rounding. I need to check to see if a variable is an integer. If it is, do nothing BUT if it's not, round down to the next whole number. How can I check to see if a variable is an integer? Thanks, Jeremy
    He who listens well, speaks well.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Use the Int() function, i.e.
    VB Code:
    1. Var = Int(Var)

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Rounding Up and Down Conditionally

    Originally posted by JCScoobyRS
    How can I check to see if a variable is an integer?
    VB Code:
    1. If Var = Int(Var) Then
    2.  MsgBox "It Is"
    3. Else
    4.  MsgBox "It Isn't"
    5. End If


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Note that what Aaron is getting at doesn't actually check whether the number is an integer or not, it just converts it to one. If you only want to round DOWN in every case (11.9 goes to 11), then this, or n\1 (integer divide by one) will work. If you want to do more than just round the value and end up with an integer, then manovo11 has got a better solution.

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    So this:

    VB Code:
    1. PPS = 1 / ItemRec!PS_QTY_P
    2. PPS = Int(PPS)

    where PPS = 17.125 doesn't round down to 17? How would I modify this to do that? Thanks, Jeremy
    He who listens well, speaks well.

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    VB Code:
    1. PPS = Int(PPS)

  7. #7
    Member
    Join Date
    Jul 2002
    Posts
    36
    If you do
    VB Code:
    1. PPS = 17.125
    and then do
    VB Code:
    1. PPS = Int(PPS)
    what is PPS set to at the end? Is it returning 17, or 18, or what? What type of variable do you have PPS dimmed as?

  8. #8

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Here is the code:

    VB Code:
    1. Private Sub Get_MHPricing(ByVal lngVvIndex As Long)
    2.     'Get Pricing for Hardware/Materials
    3.     If ItemRec2!IM_PROD_CODE = "M" Then
    4.         Dim PPS As Double
    5.         Dim SC As Long
    6.         If ItemRec!PS_QTY_P = 0 Then
    7.             PPS = 1
    8.         Else
    9.             PPS = 1 / ItemRec!PS_QTY_P
    10.             PPS = Int(PPS)
    11.         End If
    12.         If txtQuantity(lngVvIndex).Text < PPS Then
    13.             SC = 1
    14.         Else
    15.             SC = CLng(((txtQuantity(lngVvIndex).Text + 2) / PPS) + 0.49)
    16.         End If
    17.         txtMaterial(lngVvIndex).Text = txtMaterial(lngVvIndex).Text + ((SC * ItemRec2!IM_COST) / txtQuantity(lngVvIndex).Text)
    18.     ElseIf ItemRec2!IM_PROD_CODE = "H" Then
    19.         Dim QPQ As Single
    20.         QPQ = ItemRec!PS_QTY_P * txtQuantity(lngVvIndex).Text
    21.         If ItemRec2!IM_J_QTY1 = 0 Then
    22.             txtHardware(lngVvIndex).Text = ItemRec!PS_QTY_P * ItemRec2!IM_COST
    23.         Else
    24.             If QPQ < ItemRec2!IM_J_QTY1 Then
    25.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_COST)
    26.             ElseIf QPQ >= ItemRec2!IM_J_QTY1 And QPQ < ItemRec2!IM_J_QTY2 Then
    27.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_PRICES1)
    28.             ElseIf QPQ >= ItemRec2!IM_J_QTY2 And QPQ < ItemRec2!IM_J_QTY3 Then
    29.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_PRICES2)
    30.             ElseIf QPQ >= ItemRec2!IM_J_QTY3 And QPQ < ItemRec2!IM_J_QTY4 Then
    31.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_PRICES3)
    32.             ElseIf QPQ >= ItemRec2!IM_J_QTY4 And QPQ < ItemRec2!IM_J_QTY5 Then
    33.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_PRICES4)
    34.             ElseIf QPQ >= ItemRec2!IM_J_QTY5 Then
    35.                 txtHardware(lngVvIndex).Text = txtHardware(lngVvIndex).Text + (ItemRec!PS_QTY_P * ItemRec2!IM_PRICES5)
    36.             End If
    37.         End If
    38.     End If
    39. End Sub

    Thanks, Jeremy
    He who listens well, speaks well.

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