Solved comparing currency values (1 Viewer)

pasin2

New member
Local time
Today, 17:49
Joined
May 27, 2021
Messages
4
Hi,
I am writing a small program in vba for ms access. I need to write the following comparison but don't understand why it returns wrong result.
I am writing following line of code. All variables are currency type. The result should be 0 because the subtraction should give 0 as a result
Code:
day_diff=3
Debug.Print principal_tobepayed & "--" & amount & "---" & principal_payed
days(i) = IIf(day_diff > 0 And (principal_tobepayed - amount - principal_payed) <> 0, day_diff, 0)
Debug.Print principal_tobepayed - amount - principal_payed
 Debug.Print days(i)

result:
14360--14055.89---304.11
5.6843418860808E-13
3
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:49
Joined
May 7, 2009
Messages
19,233
maybe Convert to Decimal:

days(i) = IIf(day_diff > 0 And (principal_tobepayed - amount - principal_payed) <> 0, day_diff, 0)
debug.print CDec(principal_tobepayed)- CDec(amount) - CDec(principal_payed)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:49
Joined
May 7, 2009
Messages
19,233
i am also getting 0 result from:

Debug.Print principal_tobepayed - amount - principal_payed

did you Declare the variables as Currency?
 

pasin2

New member
Local time
Today, 17:49
Joined
May 27, 2021
Messages
4
i am also getting 0 result from:

Debug.Print principal_tobepayed - amount - principal_payed

did you Declare the variables as Currency?
yes they are declared as currency
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:49
Joined
Sep 21, 2011
Messages
14,235
Hmm? :unsure:
Code:
Sub testCur()
Dim cur1 As Currency, cur2 As Currency, cur3 As Currency
cur1 = 14360
cur2 = 14055.89
cur3 = 304.11
Debug.Print cur1 & " " & cur2 & " " & cur3
Debug.Print cur1 - cur2 - cur3

End Sub

Code:
14360 14055.89 304.11
 0
 

pasin2

New member
Local time
Today, 17:49
Joined
May 27, 2021
Messages
4
I found the problem..
The variables are declared as currency but they retrieve values from database that are stored as double and they are converted into double as well.

I casted to currency and now the result is ok
 

Users who are viewing this thread

Top Bottom