473,402 Members | 2,050 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,402 software developers and data experts.

Help about Round() function

I created a report of marks of students in which i want to round up marks to next whole number if are in fraction. i were used round() but it roundup 29.5 to 30 but not 30.5 to 31. In report marks alway either whole number or fractional like X.5 . So i want to round up only fractional mark to next whole number and whole number marks stay as it is.
Jun 21 '16 #1
9 1236
jforbes
1,107 Expert 1GB
I'm guessing you are running into the "Bankers Rounding" covered in How To Implement Custom Rounding Procedures

If you are looking to round up, always, no mater the fractional amount, you can use something like this:
Expand|Select|Wrap|Line Numbers
  1. Fix([YourNumber] + 1)
  2. OR
  3. Int([YourNumber] + 1)
Jun 21 '16 #2
PhilOfWalton
1,430 Expert 1GB
Sorry, that doesn't work.

Fix(10) + 1 = 11 when the required answer is 10.
Round of X.5 gives X if X is an even number and X + 1 if X is Odd

so round(10.5) = 10
round(11.5) = 12

Best solution is to add a bit say 0.00001

Expand|Select|Wrap|Line Numbers
  1. Round(YourNumber] + .00001)
  2.  
Phil
Jun 21 '16 #3
jforbes
1,107 Expert 1GB
Good call, Phil. I was hoping to be slick and missed that.

With that in mind, I would go with something like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function roundUp(ByVal x As Double) As Double
  2.     roundUp= IIf(x = Int(x), x, Int(x + 1))
  3. End Function
Expand|Select|Wrap|Line Numbers
  1. ?roundup(10)
  2. >10
  3. >?roundup(10.5)
  4. >11
Jun 21 '16 #4
PhilOfWalton
1,430 Expert 1GB
Looks even better than my solution.

Probably doesn't apply to the OP because I doubt whether negative marks are given, but your function rounds say - 10.7 to -10.0

Phil
Jun 21 '16 #5
NeoPa
32,556 Expert Mod 16PB
Phil:
Looks even better than my solution.
I'd have to disagree :-)

If the marks are always a single decimal place, as I believe the OP was trying to say, then Round(Value + 0.0001) is reliable and accurate, as well as being quite simple.

Sorry J. I believ Phil called it right first time on this one.
Jun 26 '16 #6
zmbd
5,501 Expert Mod 4TB
What about Allen's solution?
Rounding up (Read More)

To round upwards towards the next highest number, take advantage of the way Int() rounds negative numbers downwards, like this:
- Int( - [MyField])

As shown above, Int(-2.1) rounds down to -3. Therefore this expression rounds 2.1 up to 3.

To round up to the higher cent, multiply by -100, round, and divide by -100:
Int(-100 * [MyField]) / -100
So In op [Mark]=29.5
using AB;
- Int( - [Mark]) === - Int( - [29.5]) === -(-30) === 30

If [Mark]=28.2
- Int( - [Mark]) === - Int( - [28.2]) === -(-29) === 29

If [Mark]=26.0
- Int( - [Mark]) === - Int( - [26.0]) === -(-26) === 26

etc...

Of Course, this fails a bit if the fractional part is sufficiently small...
[Mark]=28.000000000000001
- Int( - [Mark]) === -Int(-28.000000000000001)
=== -(-28) === 28
Jun 27 '16 #7
jforbes
1,107 Expert 1GB
Allen has a lot of cool tricks up his sleeve. Flipping the sign is probably the least CPU intensive operation there is.
Jun 27 '16 #8
NeoPa
32,556 Expert Mod 16PB
zmbd:
What about Allen's solution?
That works fine Z. Not a lot better than using Round() with an addition of 0.5 though. In my earlier post, and in Phil's original suggestion, the basis was simply working to rounding up .5. If you're looking at the original question and want to round up any fraction then your approach will work, but is a bit fiddly (Changing sign twice as well as calling a function.) but using Round() is fraught because you'd want to map the spread of a single unit (from .0 to .999) to something that is rounded reliably. With the behaviour of Round() varying as it does that's not an option.

So, if the original question is to get correct results specifically for values ending in .0 or .5, as I believe the question is asking, then the tweaking of Round() is fine. For a more general approach your (Allen's) suggestion is required.
Jun 27 '16 #9
zmbd
5,501 Expert Mod 4TB
flipping the sign is a tad fiddly :)
however, it's a single bit operation at the processor level... shrug.

The way I distilled the OP was:
?Round(29.5) = 30 'ok
?Round(30.5) = 30 'desired result is 31
"... round up only fractional mark to next whole number ... "

What I read here is the #.5 is only an example and that the last statement intends that any fractional part was to increment the number to the next whole number in the same manner as the Excel Function
=RoundUp(30.5,0) = 31 or =RoundUp(30.02,0) = 31

Hopefully sachuchem22 will clear this up :)
Jun 27 '16 #10

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

Similar topics

3
by: mg | last post by:
Hi everybody... We try to white scripts with Pyrhon 2.4 for an acoustic simulation and we wrote these follow lines : <begin script> c = 340 i =j=k= 1 sum_ = 23 table =
3
by: ORC | last post by:
Is there a round function that do a "Normal" rounding and not the round to nearest as the Math.Rounds is? Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4....
2
by: martin | last post by:
Hi, I see that there is not round function in vb.net as the vb one has been take out. my question is what is the best function to round a number variable to a given number of decimal places....
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
5
by: Marc | last post by:
Hi, I cannot get the round function to work on vb.net. I get the message that round is not declared? Has round function changed or something? MsgBox(round(3, 3))
8
dima69
by: dima69 | last post by:
May be somebody can explain to me how Round function works in Access. This article claims VBA6 uses banker's rounding, which means that exact half rounds to the closest even digit. This article...
2
by: shashiramu | last post by:
Hi there, Am trying to round a value 36.825 to 36.82, But if i use ROUND function it is giving me 36.83 so can anyone help me in getting the value i needed Thanks in Advance Shashi
2
by: dkruger | last post by:
Hi Everyone, I am having a little problem that I am hoping someone can help me out with. In one of my scripts, I am working with some dollar values, and using round to change the result...
1
by: RiK ooo | last post by:
Hi, i'm currently working on C# project and I want to make use of the Math.Round() function to display only the first to digits of a float value. Unfortunately this doesn't seem to be working for me....
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...
0
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...
0
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,...
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
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,...

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.