VBA rounding my calculation =/

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • munkee
    Contributor
    • Feb 2010
    • 374

    VBA rounding my calculation =/

    All,

    I am doing a basic calculation as follows:

    Code:
    FiscalRatio = countfiscalsafety / countfiscalaccidents
    Debug.Print countfiscalsafety / countfiscalaccidents
    Debug.Print FiscalRatio
    The output of these debug.prints is
    Code:
     1.66666666666667 
     2
    Why is access/the vba rounding the 1.666 up to 2?

    I would like to just see the 1.66 to 2 decimal places such as 1.67 is produced.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    I would guess it's to do with the variable type of FiscalRation, but that's not included in your question so it's hard to say.

    I expect, with the necessary information, we may be able to give a fuller answer.

    Comment

    • munkee
      Contributor
      • Feb 2010
      • 374

      #3
      Code:
      Private Sub Command89_Click()
      On Error GoTo jumpout
      Dim countsafetyconcerns As Long
      Dim countaccidents As Long
      Dim countfiscalsafety As Long
      Dim countfiscalaccidents As Long
      Dim Ratio As Long
      Dim FiscalRatio As Long
      
      
      Me.txtCountAccidents = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 1)
      Me.txtCountSafetyConcerns = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 3)
      
      countsafetyconcerns = Me.txtCountSafetyConcerns
      countaccidents = Me.txtCountAccidents
      
      
      If countaccidents = 0 Then
      countaccidents = 1
      Else
      End If
      
      Ratio = countsafetyconcerns / countaccidents
      Me.txtRatio = Ratio
      
      
      Me.txtFiscalAccidents = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 1)
      Me.txtFiscalSafetyConcerns = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 3)
      
      countfiscalsafety = Me.txtFiscalSafetyConcerns
      countfiscalaccidents = Me.txtFiscalAccidents
      
      If countfiscalaccidents = 0 Then
      countfiscalaccidents = 1
      Else
      End If
      
      FiscalRatio = countfiscalsafety / countfiscalaccidents
      Debug.Print countfiscalsafety / countfiscalaccidents
      Debug.Print FiscalRatio
      Me.txtFiscalRatio = FiscalRatio
      
      completedyo:
      Exit Sub
      
      jumpout:
      MsgBox Err.Description & " - " & Err.Number
      Resume completedyo
      End Sub

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        Hi. NeoPa was spot-on here as at line 8 you've defined FiscalRatio as Long - hence the whole-number conversion. Double would be more appropriate.

        Don't know what Ratio at line 7 is used for, as it does not appear to be referred to in your code, but it also is defined as a Long. If it is going to be used as its name says to represent a ratio then it should be a Double too.

        -Stewart

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          As Stewart so correctly says, Long is explicitly integral. It's essentially an Integer type but uses 32 bits instead of just 16.

          Your code assigns the value to this Long variable, which must necessarily convert it to an integer to store it. Single & Double are floating point type variables, but depending on your requirements, you may want to look at Decimal or Currency alternatives. The help pages can highlight the benefits of each and indicate which is best for your requirements.

          Comment

          • munkee
            Contributor
            • Feb 2010
            • 374

            #6
            Dimensioning my ratios as double has fixed the issue. Thanks guys!

            Comment

            Working...