Solved

Rounding a number UP in VB

Posted on 2013-06-13
6
286 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
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 49

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 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 49

Expert Comment

by:Gustav Brock
ID: 39244146
You are welcome!

/gustav
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

919 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

21 Experts available now in Live!

Get 1:1 Help Now