Solved

Rounding a number UP in VB

Posted on 2013-06-13
6
284 Views
Last Modified: 2013-06-13
I am doing some simple calculations in the VB code of a form.

By default, the result appears to be normal rounding (eg 3.4 = 3.0, 3.6=4.0)

I need to round the result UP to the nearest 0.5.
eg 3.4 = 3.5
3.6 = 4.0

How can I do that?  Hope it's reasonably simple.

Regards

Richard
0
Comment
Question by:rltomalin
  • 3
  • 2
6 Comments
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 250 total points
Comment Utility
Assuming that you are dealing with numbers that are always >=0 then you can use ...

-(fieldname>0)*(int(fieldname*2)+1)/2

If your numbers are ALWAYS >0 then you can omit the first bit and just do...
(int(fieldname*2)+1)/2
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
It's much simpler:

dblValue = 3.4
dblRoundedUp =  -Int(-dblValue * 2) / 2

Works for negative values as well.

/gustav
0
 

Author Comment

by:rltomalin
Comment Utility
Thank you Peter
I still have a problem and I think it must be due to the way that I am handling the numbers on my form and in my code.

This statement in my code:
TotalTime = (InitialDepotTime + OutJourneyTime + StayTime + ReturnJourneyTime + FinalDepotTime) / 60
always returns a rounded value anyway. (normal rounding)

The fields are defined in the table as Number and Decimal places auto.
Also the fields on the form are Format blank and Decimal auto.
Basically these are default.

I find that even if I use a statement in my code
TotalTime = 112.54 (for example)
the form displays 113 and the value saved in the table is 113.

I think that I need a bit of coaching as to how to handle numbers in the application.

I am puzzled because I am doing other calculations on the same form, using all the default settings for the fields etc and they calculate to two decimal places.

Hope you can help out.

Best regards
Richard
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
Comment Utility
Sounds like the field holds Integer og Long. These cannot save decimals.
Adjust data type to Double or Currency.

/gustav
0
 

Author Closing Comment

by:rltomalin
Comment Utility
Thank you guys
That's sorted it out.

I have split the points - hope that is OK.

Regards

Richard
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now