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

Adding Currency 1

Status
Not open for further replies.

ynnepztem

Programmer
Aug 2, 2001
54
US
I am trying to add two amounts of currency together. I have a function that will subtract them but when I change the minus to a plus it doesn't work. Can anybody tell me what I'm doing wrong? The function that works is:

Pass in $333.33 and $111.11

Function CalculateMissing(ByVal Total As String, ByVal Found As String) As String
Dim Missing As String
Missing = FormatCurrency(Total - Found)
Return Missing
End Function

Change the "Missing = " line to
Missing = FormatCurrency(Total + Found)
and you get the error

Input string was not in a correct format.

I don't understand.
 
You could try something like this:

Code:
Missing = FormatCurrency(cdec(total.substring(1)) - cdec(found.substring(1)))

You'll probrably want to add some saftey checks (or atleast wrap it in a try/catch) just in case a zero length string, or a non-currency value is passed in.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thank you so much. It worked. I've been struggling with this for a couple of hours now.
 
Just FYI, but the reason you were getting an error using "Total + Found" is that the "+" operator for strings concatenates the strings together. So, in your example above:

Total = "333.33"
Found = "111.11"

Total + Found = "333.33111.11"

This produced the "Input string was not in a correct format" error message when you tried to format it as currency.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thanks for the additional info, Its always good to have the why added to the answers.



if it is to be it's up to me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top