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

rounding up on .5

fnwtech
48
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?
Jun 8 '10 #1

✓ answered by OldBirdman

I would use:
Expand|Select|Wrap|Line Numbers
  1. Final = ROUND((qtr1 + qtr2 + qtr3 + qtr4) / 4 + .001)

18 8006
mseo
181 100+
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
Jun 9 '10 #2
ADezii
8,834 Expert 8TB
@fnwtech
You may need some kind of 'Hybrid' Round Function, try this:
Expand|Select|Wrap|Line Numbers
  1. Public Function fNewRound(sngNumber As Single)
  2. Dim sngDecimal As Single
  3.  
  4. 'Reduce to 2 Decimal Places
  5. sngDecimal = FormatNumber(sngNumber, 2) - Fix(sngNumber)
  6.  
  7. 'Analyze the 2-Digit Decimal Component
  8. If sngDecimal >= 0.5 Then
  9.   fNewRound = Int(sngNumber) + 1
  10. Else
  11.   fNewRound = Int(sngNumber)
  12. End If
  13. End Function
Jun 9 '10 #3
fnwtech
48
@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 ;)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Final_GotFocus()
  2. Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
  3.  
  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!
Jun 9 '10 #4
ADezii
8,834 Expert 8TB
@fnwtech
Pass the Value of Final to the Function, as in:

Expand|Select|Wrap|Line Numbers
  1. Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
  2.  
  3. 'Write to a Text Box
  4. Me![txtResults] = fRoundNew(Final)
Jun 9 '10 #5
OldBirdman
675 512MB
I would use:
Expand|Select|Wrap|Line Numbers
  1. Final = ROUND((qtr1 + qtr2 + qtr3 + qtr4) / 4 + .001)
Jun 9 '10 #6
Try This:

Expand|Select|Wrap|Line Numbers
  1. Public Function RNearest(Amount As Double)
  2. Dim diceml As Double
  3. Dim intgr As Double
  4. Dim Rtotal As Double
  5. Dim dtotal As Double
  6. Dim xfinal As Double
  7. Dim final As Double
  8.  
  9. intgr = Int(Amount)
  10. Rtotal = FormatNumber(intgr, 2)
  11. diceml = Amount - intgr
  12. dtotal = diceml
  13.  
  14.   Select Case dtotal
  15.     Case Is <= 0.125
  16.         dtotal = 0
  17.     Case Is <= 0.25
  18.         dtotal = 0.25
  19.     Case Is <= 0.375
  20.         dtotal = 0.25
  21.     Case Is <= 0.5
  22.         dtotal = 0.5
  23.     Case Is <= 0.625
  24.         dtotal = 0.5
  25.     Case Is <= 0.75
  26.         dtotal = 0.75
  27.     Case Is <= 0.785
  28.         dtotal = 0.75
  29.     Case Is <= 0.999
  30.         dtotal = 1
  31.   End Select
  32.  
  33. xfinal = Rtotal + dtotal
  34. final = FormatNumber(xfinal,2)
  35. RNearest = final
  36. End Function

For Test In imeediate window write thes:
Expand|Select|Wrap|Line Numbers
  1. ?RNearest(125.335)
It`s Return To:
Expand|Select|Wrap|Line Numbers
  1. 125.25 

This equation is inadequate for dealing with cash transactions

Best Regards,

Ahmed Tharwat(Medo)
Jun 9 '10 #7
fnwtech
48
@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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Final_GotFocus()
  2. Final = (qtr1 + qtr2 + qtr3 + qtr4) / 4
  3. 'Write to a Text Box
  4. Me![txtResults] = fNewRound(Final)
  5.  
  6. End Sub
  7. Public Function fNewRound(sngNumber As Single)
  8.   Dim sngDecimal As Single
  9.  
  10.    'Reduce to 2 Decimal Places
  11.   sngDecimal = FormatNumber(sngNumber, 2) - Fix(sngNumber)
  12.  
  13.    'Analyze the 2-Digit Decimal Component
  14.  If sngDecimal >= 0.5 Then
  15.    fNewRound = Int(sngNumber) + 1
  16.   Else
  17.  fNewRound = Int(sngNumber)
  18.   End If
  19.   End Function
  20.  
  21.  
  22. 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.
Jun 9 '10 #8
NeoPa
32,556 Expert Mod 16PB
Using the function :
Expand|Select|Wrap|Line Numbers
  1. Public Function RoundIt(dblVal As Double) As Long
  2.     RoundIt = Fix(dblVal + (0.5 * Sgn(dblVal)))
  3. End Function
I ran the following command from the Immediate pane and got the results shown.
Expand|Select|Wrap|Line Numbers
  1. ?RoundIt(-1);RoundIt(-.9);RoundIt(-.8);RoundIt(-.7);RoundIt(-.6); _
  2. RoundIt(-.5);RoundIt(-.4);RoundIt(-.3);RoundIt(-.2);RoundIt(-.1); _
  3. RoundIt(0);RoundIt(.1);RoundIt(.2);RoundIt(.3);RoundIt(.4);RoundIt(.5); _
  4. RoundIt(.6);RoundIt(.7);RoundIt(.8);RoundIt(.9);RoundIt(1);
  5. -1 -1 -1 -1 -1 -1  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1
Jun 9 '10 #9
fnwtech
48
@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.
Jun 9 '10 #10
fnwtech
48
@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!
Jun 9 '10 #11
@fnwtech
Your Function Is:


Expand|Select|Wrap|Line Numbers
  1. Public Function RNerst(Amount As Double)
  2. Dim IntAmount As Double
  3. Dim DicAmount As Double
  4. IntAmount = Int(Amount)
  5. DicAmount = Amount - IntAmount
  6. If DicAmount < 0.5 Then
  7. DicAmount = 0
  8. ElseIf DicAmount >= 0.5 Then
  9. DicAmount = 1
  10. End If
  11. RNerst = IntAmount + DicAmount
  12. End Function
Try It.

(Medo)
Jun 10 '10 #12
OldBirdman
675 512MB
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.
Jun 10 '10 #13
@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:

Expand|Select|Wrap|Line Numbers
  1. Public Function RNerst(Amount As Double)
  2. Dim IntAmount As Double
  3. Dim DicAmount As Double
  4. IntAmount = Int(Amount)
  5. DicAmount = Amount - IntAmount
  6. If DicAmount < 0.5 Then
  7. DicAmount = 0
  8. ElseIf DicAmount >= 0.5 Then
  9. DicAmount = 1
  10. End If
  11. RNerst = IntAmount + DicAmount
  12. Debug.Print "Int IS: " & IntAmount
  13. Debug.Print "*********************"
  14. Debug.Print "Dic IS: " & DicAmount
  15. Debug.Print "*********************"
  16. Debug.Print "Run IS: " & IntAmount + DicAmount
  17. Debug.Print "*********************"
  18. Debug.Print "RNearst IS: " & RNerst
  19. Debug.Print "*********************"
  20. End Function
It`s Return To In Immediate Pane:

Expand|Select|Wrap|Line Numbers
  1. ?RNerst(-91.5)
  2. Int IS: -92
  3. *********************
  4. Dic IS: 1
  5. *********************
  6. Run IS: -91
  7. *********************
  8. RNearst IS: -91
  9. *********************
  10. -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:

Expand|Select|Wrap|Line Numbers
  1. Public Function RNerst(Amount As Double)
  2. Dim AAmount As Double
  3. Dim FixAmount As Double
  4. Dim DicAmount As Double
  5.  
  6. AAmount = IIf(Amount < 0, Amount * -1, Amount)
  7. FixAmount = Fix(AAmount)
  8. DicAmount = AAmount - FixAmount
  9. If DicAmount < 0.5 Then
  10. DicAmount = 0
  11. ElseIf DicAmount >= 0.5 Then
  12. DicAmount = 1
  13. End If
  14.  
  15. RRNerst = FixAmount + DicAmount
  16. RNerst = IIf(Amount < 0, RRNerst * -1, RRNerst)
  17. Debug.Print "Fix IS: " & FixAmount
  18. Debug.Print "*********************"
  19. Debug.Print "Dic IS: " & DicAmount
  20. Debug.Print "*********************"
  21. Debug.Print "Run IS: " & FixAmount + DicAmount
  22. Debug.Print "*********************"
  23. Debug.Print "RNearst IS: " & RNerst
  24. Debug.Print "*********************"
  25. End Function
It`s Return To In Immediate Pane:
Expand|Select|Wrap|Line Numbers
  1. ?RNerst(-91.5)
  2. Fix IS: 91
  3. *********************
  4. Dic IS: 1
  5. *********************
  6. Run IS: 92
  7. *********************
  8. RNearst IS: -92
  9. *********************
  10. -92 
Thank You For Let Me Thinking,i`ll Search In A Good Idea

(Medo)
Jun 10 '10 #14
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?
Jun 10 '10 #15
fnwtech
48
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 ;-)
Jun 10 '10 #16
NeoPa
32,556 Expert Mod 16PB
Not even for conduct?!!?!

I had loads of those :D
Jun 10 '10 #17
fnwtech
48
@NeoPa
yah - I am sure some teachers wish negatives were allowed!
Jun 10 '10 #18
ADezii
8,834 Expert 8TB
@NeoPa
Not even for conduct?!!?!

I had loads of those :D
Somehow I already figured that one out! (LOL)
Jun 13 '10 #19

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

Similar topics

3
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...
4
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. ...
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...
2
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 ...
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...
18
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...
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...
0
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,...
0
jinu1996
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...
0
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...
0
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...
0
isladogs
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...

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.