Jump to content

Comparing two numbers using Number function and <> operator


Recommended Posts

I experienced weird problem and hope someone can help me out.

I don't understand why I receive the error message 'Amount not equal' even though both amounts are equal. I'd like to know whether I'm using incorrect syntax or ..?

Ive compared two numbers both of them are extracted from text file. Comparing summing up the individual amount from text file with total amount which is already in that text file.

I found out that both amounts are the same on message box. I also tried to log by writing into another text file and both amounts show the same. Im using the below code

If Number($aa) <> Number($bb) Then

MsgBox(0,error, & Amount not equal )

Endif

Even though $aa and $bb are the same, it shows the error message, amount not equal. When I change the code as below it fixed the program

If NOT (Number($aa)) = (Number($bb)) Then

MsgBox(0,error, & Amount not equal )

Endif

OR

If Round(Number($aa),2) = Round(Number($bb),2) Then

MsgBox(0,error, & Amount not equal)

Endif

Thanks a lot

tawtharlay

Link to comment
Share on other sites

If the data file is not too big, or if you can reproduce the problem wih a small file sample, can you post it? You might want to masquerade any irrelevant data, if needed.

Post your code alongside so someone can reproduce or diagnose.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

If the data file is not too big, or if you can reproduce the problem wih a small file sample, can you post it? You might want to masquerade any irrelevant data, if needed.

Post your code alongside so someone can reproduce or diagnose.

Below is the sample code which I using and I attached sample text file. Thanks alot

$file = Fileopen("C:\new1.txt",0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If (@error = -1) OR $line = 'END' Then ExitLoop

MsgBox(0, "Line read:", $line)

$aa = StringLeft($line,8)

$bb = StringRignt($line,2)

$amt = Number($aa) & '.' & Number($bb)

$totamt = Round($totamt,2) + Round($amt,2)

Wend

$line = FileReadLine($file)

$a1 = StringLeft($line,8)

$b1 = StringRignt($line,2)

$amt1 = Number($aa) & '.' & Number($bb)

If Number(totamt) <> Number($amt1) then

msgbox(0,"Testing","" & "Amount is not equal")

Else

msgbox(0,"Testing","" & "Amount is equal")

Endif

below is text file example. Please save as new1.txt. Sorry unable to attach file.

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

0000099980

0000789190

0000067810

0000078810

END

0010357900

Link to comment
Share on other sites

Forgot to add say that the program is working fine for quite a while and suddently pop up that amount is not equal error message. If it is not correct, it should popup in the first place. That's why I'm quite confused. Thanks

Tawtharlay

Link to comment
Share on other sites

Well, I guess it could be a floating point issue. Strange code though. Why bother to convert each line to a float? Why not just add up the values as they are? Even if you want to continue doing what you're doing, couldn't you just divide by 100? There are numerous ways around your problem; I guess it's up to you to decide which is the best one.

WBD

Link to comment
Share on other sites

As we can see in this case, our friend tawtharlay forgot to INITIALIZE the value of $totamt variable . BEFORE entering the WHILE LOOP .

Also had some TYPOS and missing $ .

Anyway .. here you have a COLD-RUN-VAR-LIST of your routine which by the way gives AMOUNT IS NOT EQUAL.

I hope this can help ^_^

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <999.8>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <8891.7>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <9569.8>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <10357.9>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <11357.7>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <19249.6>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <19927.7>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <20715.8>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <21715.6>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <29607.5>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <30285.6>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <31073.7>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <32073.5>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <39965.4>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <40643.5>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <41431.6>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <42431.4>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <50323.3>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <51001.4>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <51789.5>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <52789.3>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <60681.2>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <61359.3>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <62147.4>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <63147.2>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <71039.1>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <71717.2>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <72505.3>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <73505.1>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <81397>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <82075.1>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <82863.2>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <83863>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <91754.9>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <92433>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <93221.1>

*************************************

$line: <0000099980>

$aa: <00000999>

$bb: <80>

$amt: <999.80>

$totamt: <94220.9>

*************************************

$line: <0000789190>

$aa: <00007891>

$bb: <90>

$amt: <7891.90>

$totamt: <102112.8>

*************************************

$line: <0000067810>

$aa: <00000678>

$bb: <10>

$amt: <678.10>

$totamt: <102790.9>

*************************************

$line: <0000078810>

$aa: <00000788>

$bb: <10>

$amt: <788.10>

$totamt: <103579>

*************************************

$line: <END>

$line: <0010357900>

$a1: <00103579>

$b1: <00>

$amt1: <788.10>

$totamt: <103579>

$amt1: <788.10>

[s][font="Impact"]░▒▓▓►DrKovra◄▓▓▒░[/font][/s]The only thing I [sup]know [/sup]is that I don't know [sub]nothing[/sub]--------------- __________------------------------------ __________---------------

Link to comment
Share on other sites

btw ... JUST change $a1 for $aa and $b1 for $bb ... and THE AMOUNT IS EQUAL !!!! TADAAAAA !

^_^

$file = Fileopen("C:\new1.txt",0) 
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

$totamt=0

While 1
$line = FileReadLine($file)

If (@error = -1) OR $line = 'END' Then ExitLoop
;~ MsgBox(0, "Line read:", $line,1)
$aa = StringLeft($line,8)

$bb = StringRight($line,2)

$amt = Number($aa) & '.' & Number($bb) 


$totamt = Round($totamt,2) + Round($amt,2)

Wend

$line = FileReadLine($file)

$aa = StringLeft($line,8)

$bb = StringRight($line,2)

$amt1 = Number($aa) & '.' & Number($bb)

If Number($totamt) <> Number($amt1) then
msgbox(0,"Testing","" & "Amount is not equal")
Else
msgbox(0,"Testing","" & "Amount is equal")
Endif

...

[s][font="Impact"]░▒▓▓►DrKovra◄▓▓▒░[/font][/s]The only thing I [sup]know [/sup]is that I don't know [sub]nothing[/sub]--------------- __________------------------------------ __________---------------

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...