473,385 Members | 1,329 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,385 software developers and data experts.

ROUNDING to nearest 500

2
Hi there,

Can anyone help me sort out a rounding problem with Access?

I am trying to round a value in a field up or down to the nearest 500.
Expand|Select|Wrap|Line Numbers
  1. [PriceNew] * [PercentDep] = [CurrentValue]
I want the current value field to round up or down to the nearest 500. I am using the following script, which works for the most part, except that when the [CurrentValue] is 500, 1500, 2500, etc., it will round it up to the next 1000. At 2500, it is already rounded to the nearest 500, so I don't want it to jump again.

Here is the script I am currently using:
Expand|Select|Wrap|Line Numbers
  1. Dim I As Integer
  2. Dim R As Integer
  3. Dim HR As Integer
  4. Dim AD As Integer
  5. Dim Div As Integer
  6. Div = 100
  7. I = Me.CurrentValue
  8. R = (Round(I / (Div * 10), 2))
  9.  
  10. HR = Me.CurrentValue - R * (Div * 10)
  11.  
  12. If (HR / Div) <> 5 Then
  13.  
  14.    If (HR / Div) > 2.5 Then
  15.        AD = 5 * Div
  16.  
  17.    Else
  18.        AD = 0
  19.    End If
  20.  
  21. End If
  22.  
  23. Me.Text32 = (R * (Div * 10)) + AD
  24. End If
  25.  
  26.  
  27. End Sub
Thanks,
Nel
Jan 20 '08 #1
3 8479
Check out the mod function in VBA. It will make coding this much simpler. I found the following description online:

"The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. The remainder operation is performed with keyword Mod. Its syntax is:

Value1 Mod Value2"

Basically, I would use the int function to determine how many times 500 goes into your number. I would then use the mod function to round up or down. For example:

Expand|Select|Wrap|Line Numbers
  1. Function RoundTo500(Num As Integer)
  2.  
  3. Dim rndNum As Integer
  4. Dim newNum As Double
  5.  
  6. rndNum = 500
  7.  
  8. newNum = rndNum * (Int(Num / rndNum))
  9.  
  10. If Num Mod rndNum > rndNum / 2 Then newNum = newNum + rndNum
  11.  
  12. RoundTo500 = newNum
  13.  
  14. End Function
  15.  
Jan 20 '08 #2
Nel222
2
Check out the mod function in VBA. It will make coding ...
Thanks for your response. I haven't tried it yet because I had found another solution that seemed to work. I am posting it here in case you may be interested.

From Tek-Tips Forum:

mugs132 (MIS) 21 Nov 05 14:38
I'm an Accounting Manager at a large steel company. I need the most versatile rounding function I can get.

I found this function and added the "nearest" function so it can now round any way you could possibly want...UP..DOWN..NEAREST. It mimicks MS Excel rounding so you'll never have to explain any differences there.

Example of "rounding up" to the closest nickel or dime in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],.05,"UP")


Example of rounding to the nearest 100 in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],100,"NEAREST")

ENJOY!!!!!!!
Expand|Select|Wrap|Line Numbers
  1. Public Function RND_TO_NEAREST(Amt As Variant, Divisor As Variant, DIR_UP_DN_NEAREST As String) As Variant
  2.  
  3.     On Error Resume Next
  4.     Dim Temp As Variant
  5.     Temp = (Amt / Divisor)
  6.     If Int(Temp) = Temp Then
  7.         RND_TO_NEAREST = Amt
  8.         Exit Function
  9.  
  10.     Else
  11.         Select Case UCase(DIR_UP_DN_NEAREST)
  12.             Case "UP"
  13.                 Temp = Int(Temp) + 1
  14.                 RND_TO_NEAREST = Temp * Divisor
  15.                 Exit Function
  16.             Case "DN"
  17.                 Temp = Int(Temp)
  18.                 RND_TO_NEAREST = Temp * Divisor
  19.                 Exit Function
  20.             Case "NEAREST"
  21.                 Temp = Round(Amt / Divisor) * Divisor
  22.                 RND_TO_NEAREST = Temp
  23.                 Exit Function
  24.             Case Else
  25.                 Exit Function
  26.         End Select
  27.     End If
  28. End Function
Thanks,
Doug
Jan 21 '08 #3
ADezii
8,834 Expert 8TB
Hi there,

Can anyone help me sort out a rounding problem with Access?

I am trying to round a value in a field up or down to the nearest 500.

[PriceNew] * [PercentDep] = [CurrentValue]

I want the current value field to round up or down to the nearest 500. I am using the following script, which works for the most part, except that when the [CurrentValue] is 500, 1500, 2500, etc., it will round it up to the next 1000. At 2500, it is already rounded to the nearest 500, so I don't want it to jump again.

Here is the script I am currently using:

Dim I As Integer
Dim R As Integer
Dim HR As Integer
Dim AD As Integer
Dim Div As Integer
Div = 100
I = Me.CurrentValue
R = (Round(I / (Div * 10), 2))

HR = Me.CurrentValue - R * (Div * 10)

If (HR / Div) <> 5 Then

If (HR / Div) > 2.5 Then
AD = 5 * Div

Else
AD = 0
End If

End If

Me.Text32 = (R * (Div * 10)) + AD
End If


End Sub


Thanks,
Nel
I threw this together along with some sample output before 'going to bed', so you better check it carefully (LOL):
Expand|Select|Wrap|Line Numbers
  1. Dim varNumToRound As Variant, intRemainder As Integer
  2. Dim lngNumRoundedTo500 As Long, intIncrement As Integer
  3.  
  4. Dim lngNumOf500s As Long
  5. varNumToRound = InputBox("Enter Number to Round", "Round")
  6.  
  7. lngNumOf500s = Fix(varNumToRound / 500)
  8. intRemainder = varNumToRound Mod 500
  9.  
  10. If intRemainder < 250 Then
  11.   intIncrement = 0
  12. Else
  13.   intIncrement = 500
  14. End If
  15.  
  16. lngNumRoundedTo500 = (lngNumOf500s * 500) + intIncrement
  17.  
  18. Debug.Print "Number: " & varNumToRound & vbCrLf & "Number of 500s: " & lngNumOf500s & vbCrLf & _
  19.             "Remainder: " & intRemainder & vbCrLf & _
  20.             "Number Rounded: " & Format$(lngNumRoundedTo500, "#,#,#,#")
Sample OUTPUTS:
Expand|Select|Wrap|Line Numbers
  1. Number: 1500
  2. Number of 500s: 3
  3. Remainder: 0
  4. Number Rounded: 1,500
  5.  
  6. Number: 32456
  7. Number of 500s: 64
  8. Remainder: 456
  9. Number Rounded: 32,500
  10.  
  11. Number: 987654
  12. Number of 500s: 1975
  13. Remainder: 154
  14. Number Rounded: 987,500
  15.  
  16. Number: 97321
  17. Number of 500s: 194
  18. Remainder: 321
  19. Number Rounded: 97,500
  20.  
  21. Number: 497116
  22. Number of 500s: 994
  23. Remainder: 116
  24. Number Rounded: 497,000
  25.  
  26. Number: 1999765332
  27. Number of 500s: 3999530
  28. Remainder: 332
  29. Number Rounded: 1,999,765,500
Jan 21 '08 #4

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

Similar topics

3
by: b | last post by:
Hello all, we have a table that is part of our accounting package that stores decimals as such: 123.45678. However the reporting standards with the accounting system display that as 123.45 note...
8
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
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 ---------------------...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
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) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.