Using Access 2003
I am using the following expression in a reportcard db:
Final=(qt1+qtr2+qtr3+qtr4)/4
The field is a Number Field set to integer and decimal places set to 0
Most of the time it rounds correctly. However, when the answer has .5 (such as 98.5) it rounds down to 98 instead of up to 99.
Is there a fairly easy way to fix this so that .5 always rounds up?
I would use: - Final = ROUND((qtr1 + qtr2 + qtr3 + qtr4) / 4 + .001)
18 8006
hi,
the attached file contains the function and how to use it within query or form, it will let you round the numbers up or down
hope this helps
@fnwtech
You may need some kind of 'Hybrid' Round Function, try this: - Public Function fNewRound(sngNumber As Single)
-
Dim sngDecimal As Single
-
-
'Reduce to 2 Decimal Places
-
sngDecimal = FormatNumber(sngNumber, 2) - Fix(sngNumber)
-
-
'Analyze the 2-Digit Decimal Component
-
If sngDecimal >= 0.5 Then
-
fNewRound = Int(sngNumber) + 1
-
Else
-
fNewRound = Int(sngNumber)
-
End If
-
End Function
@ADezii
ADezii - can you tell me where I place this? Do I place it somewhere within this statement? Tried after the End sub and of course it ignores it ;) - Private Sub Final_GotFocus()
-
Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
-
-
End Sub
mseo - I am looking to round up when it is .5 not to the nearest 5...(e.g 92.5 round to 93) Currently, something to do with bankers math, Access rounds even numbers down and odd numbers up...
Thanks!
@fnwtech
Pass the Value of Final to the Function, as in: - Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
-
-
'Write to a Text Box
-
Me![txtResults] = fRoundNew(Final)
I would use: - Final = ROUND((qtr1 + qtr2 + qtr3 + qtr4) / 4 + .001)
Try This: - Public Function RNearest(Amount As Double)
-
Dim diceml As Double
-
Dim intgr As Double
-
Dim Rtotal As Double
-
Dim dtotal As Double
-
Dim xfinal As Double
-
Dim final As Double
-
-
intgr = Int(Amount)
-
Rtotal = FormatNumber(intgr, 2)
-
diceml = Amount - intgr
-
dtotal = diceml
-
-
Select Case dtotal
-
Case Is <= 0.125
-
dtotal = 0
-
Case Is <= 0.25
-
dtotal = 0.25
-
Case Is <= 0.375
-
dtotal = 0.25
-
Case Is <= 0.5
-
dtotal = 0.5
-
Case Is <= 0.625
-
dtotal = 0.5
-
Case Is <= 0.75
-
dtotal = 0.75
-
Case Is <= 0.785
-
dtotal = 0.75
-
Case Is <= 0.999
-
dtotal = 1
-
End Select
-
-
xfinal = Rtotal + dtotal
-
final = FormatNumber(xfinal,2)
-
RNearest = final
-
End Function
For Test In imeediate window write thes:
It`s Return To: This equation is inadequate for dealing with cash transactions
Best Regards,
Ahmed Tharwat( Medo)
@ADezii
First, you used fRoundNew here but fNewRound above, Should they be the same?
So,I changed both to fNewRound so this is what I have: - Private Sub Final_GotFocus()
-
Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
-
'Write to a Text Box
-
Me![txtResults] = fNewRound(Final)
-
-
End Sub
-
Public Function fNewRound(sngNumber As Single)
-
Dim sngDecimal As Single
-
-
'Reduce to 2 Decimal Places
-
sngDecimal = FormatNumber(sngNumber, 2) - Fix(sngNumber)
-
-
'Analyze the 2-Digit Decimal Component
-
If sngDecimal >= 0.5 Then
-
fNewRound = Int(sngNumber) + 1
-
Else
-
fNewRound = Int(sngNumber)
-
End If
-
End Function
-
-
-
End Function
Now I am receiving an error that nothing but comments can follow End Sub... so I guess I am placing this in the wrong place.
NeoPa 32,556
Expert Mod 16PB
Using the function : - Public Function RoundIt(dblVal As Double) As Long
-
RoundIt = Fix(dblVal + (0.5 * Sgn(dblVal)))
-
End Function
I ran the following command from the Immediate pane and got the results shown. - ?RoundIt(-1);RoundIt(-.9);RoundIt(-.8);RoundIt(-.7);RoundIt(-.6); _
-
RoundIt(-.5);RoundIt(-.4);RoundIt(-.3);RoundIt(-.2);RoundIt(-.1); _
-
RoundIt(0);RoundIt(.1);RoundIt(.2);RoundIt(.3);RoundIt(.4);RoundIt(.5); _
-
RoundIt(.6);RoundIt(.7);RoundIt(.8);RoundIt(.9);RoundIt(1);
-
-1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
@fnwtech
Okay, realized I had to End functions...
So now can you tell me what the Me![txtResults] needs to be pointing to? Is this another field I need to add? That is now where it is hanging.
@OldBirdman
OldBirdman - this one seems to work for what I need it to do. I have tested results that give 90.5 (gives 91)and 91.5 (gives 92) also 91.25 stays a 91... so this seems to do the trick.
Thanks everyone for all your assistance on this!
@fnwtech
Your Function Is: - Public Function RNerst(Amount As Double)
-
Dim IntAmount As Double
-
Dim DicAmount As Double
-
IntAmount = Int(Amount)
-
DicAmount = Amount - IntAmount
-
If DicAmount < 0.5 Then
-
DicAmount = 0
-
ElseIf DicAmount >= 0.5 Then
-
DicAmount = 1
-
End If
-
RNerst = IntAmount + DicAmount
-
End Function
Try It.
( Medo)
Good. If negative numbers are possible, then NeoPa's post #9 should be considered. The Sgn() function MAY BE needed if 'to round up' means that the absolute value is larger. If the actual value is higher, my solution is good for negative numbers.
In other words, when you round -91.5, do you expect -91 or -92.
@OldBirdman
In other words, when you round -91.5, do you expect -91 or -92.
I Expect It Should Be -91.
Because When We Increase The Minus The Value Is decrease.
In My Function in #12
eq. say:
-91.5 = -91 after round up
-91.5 = -91 + 1
-91.5 = -91
To Explain It In My Function do Follwing: - Public Function RNerst(Amount As Double)
-
Dim IntAmount As Double
-
Dim DicAmount As Double
-
IntAmount = Int(Amount)
-
DicAmount = Amount - IntAmount
-
If DicAmount < 0.5 Then
-
DicAmount = 0
-
ElseIf DicAmount >= 0.5 Then
-
DicAmount = 1
-
End If
-
RNerst = IntAmount + DicAmount
-
Debug.Print "Int IS: " & IntAmount
-
Debug.Print "*********************"
-
Debug.Print "Dic IS: " & DicAmount
-
Debug.Print "*********************"
-
Debug.Print "Run IS: " & IntAmount + DicAmount
-
Debug.Print "*********************"
-
Debug.Print "RNearst IS: " & RNerst
-
Debug.Print "*********************"
-
End Function
It`s Return To In Immediate Pane: - ?RNerst(-91.5)
-
Int IS: -92
-
*********************
-
Dic IS: 1
-
*********************
-
Run IS: -91
-
*********************
-
RNearst IS: -91
-
*********************
-
-91
---------------------------------------------------------- OldBirdman,
But your words in any of the really puzzling, some equations and figures put some other figures put
This is another attempt on that: - Public Function RNerst(Amount As Double)
-
Dim AAmount As Double
-
Dim FixAmount As Double
-
Dim DicAmount As Double
-
-
AAmount = IIf(Amount < 0, Amount * -1, Amount)
-
FixAmount = Fix(AAmount)
-
DicAmount = AAmount - FixAmount
-
If DicAmount < 0.5 Then
-
DicAmount = 0
-
ElseIf DicAmount >= 0.5 Then
-
DicAmount = 1
-
End If
-
-
RRNerst = FixAmount + DicAmount
-
RNerst = IIf(Amount < 0, RRNerst * -1, RRNerst)
-
Debug.Print "Fix IS: " & FixAmount
-
Debug.Print "*********************"
-
Debug.Print "Dic IS: " & DicAmount
-
Debug.Print "*********************"
-
Debug.Print "Run IS: " & FixAmount + DicAmount
-
Debug.Print "*********************"
-
Debug.Print "RNearst IS: " & RNerst
-
Debug.Print "*********************"
-
End Function
It`s Return To In Immediate Pane: - ?RNerst(-91.5)
-
Fix IS: 91
-
*********************
-
Dic IS: 1
-
*********************
-
Run IS: 92
-
*********************
-
RNearst IS: -92
-
*********************
-
-92
Thank You For Let Me Thinking,i`ll Search In A Good Idea
( Medo)
NeoPa 32,556
Expert Mod 16PB ahmedtharwat19: I Expect It Should Be -91.
You may well expect that Medo :D
I suspect the question was directed at the OP (Original Poster - The titular owner of the thread) however.
It is possible that your interpretation is correct, though I would have to say it is unlikely in my experience. Generally, for consistency, the values above zero should reflect those below. Standard default rounding is to round up when the absolute value of the fractional part of a real number is .5 or greater.
The important question though, is what does fnwtech require?
I actually never gave any thought to negative numbers, nor does it matter in my case. Being a middle school report card I hope there are no negative scores ;-)
NeoPa 32,556
Expert Mod 16PB
Not even for conduct?!!?!
I had loads of those :D
@NeoPa
yah - I am sure some teachers wish negatives were allowed!
@NeoPa
Not even for conduct?!!?!
I had loads of those :D
Somehow I already figured that one out! (LOL)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Norvin Laudon |
last post by:
Hi,
Can somebody explain the following, from the MSDN documentation for the
"System.Convert.ToInt32(double)" function
<quote>
Return Value
value rounded to the nearest 32-bit signed...
|
by: spebola |
last post by:
I am using vb.net 2003 professional and I get the following results
when using the round method:
dim Amount as decimal = 180.255
Amount = Amount.Round(Amount, 2)
Amount now contains 180.25. ...
|
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...
|
by: Jiri Nemec |
last post by:
Hello all,
I have got one table with rounding values, table contains
prices and round types.
id price_from price_to rounding
1 0 1500 0.1
2 1500 ...
|
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...
|
by: jdrott1 |
last post by:
i'm trying to round my currency string to end in 9. it's for a
pricing application.
this is the function i'm using to get the item in currency:
FormatCurrency(BoxCost, , , , TriState.True)
if...
|
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...
|
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)
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |