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
3 8516
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: -
Function RoundTo500(Num As Integer)
-
-
Dim rndNum As Integer
-
Dim newNum As Double
-
-
rndNum = 500
-
-
newNum = rndNum * (Int(Num / rndNum))
-
-
If Num Mod rndNum > rndNum / 2 Then newNum = newNum + rndNum
-
-
RoundTo500 = newNum
-
-
End Function
-
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!!!!!!! - Public Function RND_TO_NEAREST(Amt As Variant, Divisor As Variant, DIR_UP_DN_NEAREST As String) As Variant
-
-
On Error Resume Next
-
Dim Temp As Variant
-
Temp = (Amt / Divisor)
-
If Int(Temp) = Temp Then
-
RND_TO_NEAREST = Amt
-
Exit Function
-
-
Else
-
Select Case UCase(DIR_UP_DN_NEAREST)
-
Case "UP"
-
Temp = Int(Temp) + 1
-
RND_TO_NEAREST = Temp * Divisor
-
Exit Function
-
Case "DN"
-
Temp = Int(Temp)
-
RND_TO_NEAREST = Temp * Divisor
-
Exit Function
-
Case "NEAREST"
-
Temp = Round(Amt / Divisor) * Divisor
-
RND_TO_NEAREST = Temp
-
Exit Function
-
Case Else
-
Exit Function
-
End Select
-
End If
-
End Function
Thanks,
Doug
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): -
Dim varNumToRound As Variant, intRemainder As Integer
-
Dim lngNumRoundedTo500 As Long, intIncrement As Integer
-
-
Dim lngNumOf500s As Long
-
varNumToRound = InputBox("Enter Number to Round", "Round")
-
-
lngNumOf500s = Fix(varNumToRound / 500)
-
intRemainder = varNumToRound Mod 500
-
-
If intRemainder < 250 Then
-
intIncrement = 0
-
Else
-
intIncrement = 500
-
End If
-
-
lngNumRoundedTo500 = (lngNumOf500s * 500) + intIncrement
-
-
Debug.Print "Number: " & varNumToRound & vbCrLf & "Number of 500s: " & lngNumOf500s & vbCrLf & _
-
"Remainder: " & intRemainder & vbCrLf & _
-
"Number Rounded: " & Format$(lngNumRoundedTo500, "#,#,#,#")
Sample OUTPUTS: -
Number: 1500
-
Number of 500s: 3
-
Remainder: 0
-
Number Rounded: 1,500
-
-
Number: 32456
-
Number of 500s: 64
-
Remainder: 456
-
Number Rounded: 32,500
-
-
Number: 987654
-
Number of 500s: 1975
-
Remainder: 154
-
Number Rounded: 987,500
-
-
Number: 97321
-
Number of 500s: 194
-
Remainder: 321
-
Number Rounded: 97,500
-
-
Number: 497116
-
Number of 500s: 994
-
Remainder: 116
-
Number Rounded: 497,000
-
-
Number: 1999765332
-
Number of 500s: 3999530
-
Remainder: 332
-
Number Rounded: 1,999,765,500
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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
|
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
| |
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
|
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
|
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
|
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>
|
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 ?
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |