?
Solved

Rounding a number UP in VB

Posted on 2013-06-13
6
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 1000 total points
ID: 39243991
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 51

Expert Comment

by:Gustav Brock
ID: 39244091
It's much simpler:

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

Works for negative values as well.

/gustav
0
 

Author Comment

by:rltomalin
ID: 39244093
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 1000 total points
ID: 39244103
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
ID: 39244128
Thank you guys
That's sorted it out.

I have split the points - hope that is OK.

Regards

Richard
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 39244146
You are welcome!

/gustav
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

770 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