Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rounding a number UP in VB

Posted on 2013-06-13
6
Medium Priority
?
303 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 52

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 52

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 52

Expert Comment

by:Gustav Brock
ID: 39244146
You are welcome!

/gustav
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 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