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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rounding 3

Status
Not open for further replies.

elle1

Programmer
Jul 31, 2002
3
US
I need to be able to do as in excel the round_down function in visual basic6.0

I have a number 2.6677 to round to 2.667 and not 2.668


 
This function should do the trick. Just call it with the number you wish to round down and the number of values after the decimal place you wish.

Private Function RoundDown(dblNum As Double, intPlacesAfterDecimal As Integer) As Double
RoundDown = CDbl(Left(CStr(dblNum), intPlacesAfterDecimal + 2))
End Function


Hope this helps,

Keith
 

Fix(2.6677 * (10 ^ 3)) / (10 ^ 3)

Replace the 3 with the number of places you need to round down using a literal number like above or a variable

Public Function RoundDown(dNumber As Double, Optional ByVal lDecimalPlaces As Long = 2) As Double

RoundDown = Fix(dNumber * (10 ^ lDecimalPlaces)) / (10 ^ lDecimalPlaces)

End Function

Then call it like this:
Dim dNumber As Double
dNumber = 2.6677
Debug.Print RoundDown(dNumber ,3)

You can change the Optional parameter from 2 to 3 if you need to round to that more often, in which case you can just call it as:

Debug.Print RoundDown(dNumber)



 
IAM A NOVICE TO VB, KEITH, WHERE DO I PUT THE FUNCTION, IN FORM?
 
There are several different options depeding on what you want to do.

Probably the best option, if you a novice, would be to simply copy and paste the code into the form you wish to call the function from.

If your feeling brave you could try putting into a module or class file and making it public so you can access it from other forms and classes, but if your not sure how to do that stick with the first method.

Also the last method suggested by CCLINT will work move flexibly than the method I suggested so I would recommend using that.
 
THANK YOU EVERYONE FOR YOUR HELP.
ELLE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top