Solved

# Rounding a number UP in VB

Posted on 2013-06-13
284 Views
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
Question by:rltomalin
• 3
• 2

LVL 77

Assisted Solution

peter57r earned 250 total points
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

It's much simpler:

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

Works for negative values as well.

/gustav
0

Author Comment

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

LVL 49

Accepted Solution

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

/gustav
0

Author Closing Comment

Thank you guys
That's sorted it out.

I have split the points - hope that is OK.

Regards

Richard
0

LVL 49

Expert Comment

You are welcome!

/gustav
0

## Featured Post

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.