Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding and...

Status
Not open for further replies.

jalgier

Programmer
Nov 7, 2001
15
US
Greetings,

Two quick questions...on a form I have, I need to round both up and down on certain values (regardless of what the decimal value is). For instance, I might need 3.8 to round DOWN to 3 or 4.01 to round UP to 5.

Second question, how do I select the value to the left of the decimal (or any value for that matter). I seem to recall seeing a function that will allow you to select the values to left (or right) of a pre-determined number or character.
Example: Variable = everything left of "." So 21.925 would give you 21. Any help or hints are appreciated. Thanks
 
Rounding to nearest integer can be done by Cint
Dim MyDouble, MyInt
MyDouble = 2345.5678 ' MyDouble is a Double.
MyInt = CInt(MyDouble) ' MyInt contains 2346.

There are various rounding routines - search Microsofts KnowledgeBase by keyword and you should come up with some good examples including sample code.
 
The rounding function to a specified number of DPs is:-

(CInt((YourNumber * (10^NumberOfDPs))+0.5))/(10^NumberOfDPs)

Works like this:

YourNumber = 12345.678
NumberOfDPs = 2

YourNumber * (10^NumberOfDPs) = 1234567.8
YourNumber * (10^NumberOfDPs) + 0.5 = 1234568.3
CInt(YourNumber * (10^NumberOfDPs) + 0.5) = 1234568
(CInt((YourNumber * (10^NumberOfDPs))+0.5))/(10^NumberOfDPs) = 12345.68

Craig
 
Excellent. The logic works. Thank you both for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top