473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ROUNDING to nearest 500

2 New Member
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 8516
doulostheou
2 New Member
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 New Member
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 Recognized Expert Expert
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
332
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 that it does not round to the 123.46. I need for these reports to match up however in my datagrid which i have set for currency it does the rounding.. Does anyone know a way around this.. I Have looked around on the web but yet to find anything...
8
2088
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 they thinking??? I just don't
6
4609
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 --------------------- 2004-05-27 09:00:01
12
13655
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 put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result will be 419
11
6663
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 seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
29
3194
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
8018
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 raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not
206
13347
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) It then updates the value with numberOfPrecisions after the decimal
20
5023
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
29701
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 expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.