473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

VBA rounding my calculation =/

374 256MB
All,

I am doing a basic calculation as follows:

Expand|Select|Wrap|Line Numbers
  1. FiscalRatio = countfiscalsafety / countfiscalaccidents
  2. Debug.Print countfiscalsafety / countfiscalaccidents
  3. Debug.Print FiscalRatio
The output of these debug.prints is
Expand|Select|Wrap|Line Numbers
  1.  1.66666666666667 
  2.  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.
Jan 8 '11 #1

✓ answered by Stewart Ross

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

5 2367
NeoPa
32,556 Expert Mod 16PB
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.
Jan 8 '11 #2
munkee
374 256MB
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command89_Click()
  2. On Error GoTo jumpout
  3. Dim countsafetyconcerns As Long
  4. Dim countaccidents As Long
  5. Dim countfiscalsafety As Long
  6. Dim countfiscalaccidents As Long
  7. Dim Ratio As Long
  8. Dim FiscalRatio As Long
  9.  
  10.  
  11. Me.txtCountAccidents = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 1)
  12. Me.txtCountSafetyConcerns = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 3)
  13.  
  14. countsafetyconcerns = Me.txtCountSafetyConcerns
  15. countaccidents = Me.txtCountAccidents
  16.  
  17.  
  18. If countaccidents = 0 Then
  19. countaccidents = 1
  20. Else
  21. End If
  22.  
  23. Ratio = countsafetyconcerns / countaccidents
  24. Me.txtRatio = Ratio
  25.  
  26.  
  27. Me.txtFiscalAccidents = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 1)
  28. Me.txtFiscalSafetyConcerns = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 3)
  29.  
  30. countfiscalsafety = Me.txtFiscalSafetyConcerns
  31. countfiscalaccidents = Me.txtFiscalAccidents
  32.  
  33. If countfiscalaccidents = 0 Then
  34. countfiscalaccidents = 1
  35. Else
  36. End If
  37.  
  38. FiscalRatio = countfiscalsafety / countfiscalaccidents
  39. Debug.Print countfiscalsafety / countfiscalaccidents
  40. Debug.Print FiscalRatio
  41. Me.txtFiscalRatio = FiscalRatio
  42.  
  43. completedyo:
  44. Exit Sub
  45.  
  46. jumpout:
  47. MsgBox Err.Description & " - " & Err.Number
  48. Resume completedyo
  49. End Sub
Jan 8 '11 #3
Stewart Ross
2,545 Expert Mod 2GB
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
Jan 8 '11 #4
NeoPa
32,556 Expert Mod 16PB
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.
Jan 8 '11 #5
munkee
374 256MB
Dimensioning my ratios as double has fixed the issue. Thanks guys!
Jan 8 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Allen Thompson | last post by:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
2
by: Deano | last post by:
One thing that slightly annoys me about my app is a side-affect from the way it processes figures. It calculates salaries very precisely (to the penny) but there are rounding issues that cause...
2
by: Lynn N. | last post by:
I have a report showing Rate, Hours and Total Pay (which is Rate*Hours) for several workers. I want to sum the Total Pay and get a CORRECT figure. This seems like it should be such a simple task....
9
by: Klaus Bonadt | last post by:
I have found strange behaviour in casting floating point values in C++ 6.0 to int: If I enter in the watch window while debugging in version 6.0 the following term: (1.4 - 1.0) * 10.0 the...
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
5
by: Cygnus | last post by:
Sorry in advance for the lack of formatting in this posting. Data: (column headers) Net Sales | Royalty Rate | Total Royalty (data) 4.31 | 50.00% | 2.15 19.35 | 50.00% | 9.68
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
2
by: rak53820 | last post by:
pls give java code for this Calculation : Premium for 1 year (For members who have joined the group before policy issuance): Basic Premium (A) = (Basic Rate * SA/1000 or SA/100000Round...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.