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

Error in Function 1

Status
Not open for further replies.

dodgyone

Technical User
Jan 26, 2001
431
GB
I'm having problems with this function. For some reason it fails on the line:

Code:
strMerged = Mid(strTotal, 1, intPosition - 1) & Mid(strTotal, intPosition, 3)

My entire function:

Code:
Function RoundN(curTotal As Currency, N As Integer) As Currency

Dim strTotal As String
Dim intPosition As Integer
Dim strMerged As String
Dim curConverted As Currency

strTotal = CStr(curTotal)
intPosition = InStr(1, strTotal, ".")
strMerged = Mid(strTotal, 1, intPosition - 1) & Mid(strTotal, intPosition, 3)
curConverted = CCur(strMerged)
RoundN = curConverted

End Function
 
I'm not trying to remove the full stop. I want to convert to a string and then remove the third decimal onwards leaving only two decimals. Convert back to currency and retrun the value.

I'm wanting to avoid rounding up the total as my boss doesn't want that to happen. He just wants the two decimal places and ignore the rest that follows.
 
Such math is not my strong side, but what about keeping it numeric, and try something like

[tt]RoundN = fix(curTotal * 10^N)/10^N[/tt]

- note, check the behaviour on negative numbers, and try the int() function in stead of fix(), if not correct

Roy-Vidar
 
Thanks for the idea Roy-Vidar. A star for you.
 
You may also try this:
Function RoundN(curTotal As Currency, N As Integer) As Currency
RoundN = CCur(Format(curTotal, "0." & String(N, "0")))
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top