Number not evaluating as expected

Started by 1bosborn, October 03, 2014, 10:34:55 AM

Previous topic - Next topic


Here is an excerpt from my script:
   gross = ItemExtract(4, inline, @Tab)
   faxno = ""
   If gross >= '15000' Then category = 'A'
   If gross >= '5000' && gross <= '14999' Then category = 'B'
   If gross >= '1000' && gross <= '4999' Then Category = 'C'
The gross for the item in question is 49,241.15. The first and second If statements return true and false respectively as expected but the 3rd one also returns true making the category for the item "C" rather that the "A" it should be. Any thoughts on why 49,241.15 would evaluate between 1000 and 4999?
For diagnostic purposes, I modified the code to:
   gross = ItemExtract(4, inline, @Tab)
   faxno = ""
   If gross >= '15000' Then category = 'A' ;this came back true (expected)
   If gross >= '5000'                      ;this came back false (Unexpected)
      If gross <= '14999' Then category = 'B' ;this was skipped
   If gross >= '1000'                      ;this cam back true (expected)
      If gross <= '4999' Then category = 'C'  ;this came back true (unexpected)
I have included comments as to how each evaluated


You are treating them as text rather than numbers and a comma is lower on the ASCII chart than a 9 so 49,241.15 is less than 4999 but greater than 1000.



Interesting, thank you so much for the prompt reply. How do I stop treating it as text and make it a number, remove the comma? And if I were to put a comma into the "text" I am evaluating against would you think it would work as expected?


User StrClean

Code (winbatch) Select
  gross=StrClean( gross, ", ", "", 0, 1)

to remove comas and get rid of the single quotes around your numeric literals. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade


Commas will not work in "numbers".



As a matter of style, I'd use SWITCH instead of a series of IF statements.

Try this code:

; vim:fo-=t fo+=ro1 comments+=b\:;
IntControl(50,0,0,0,0)      ; Turn off Web Page Support

Prog = IntControl(1004,0,0,0,0)
gross = "15,123.45"

gross = AskLine(Prog,"Enter value for Gross:",gross)
cleanGross = StrClean(gross, ", ", "", 0, 1)
CASE cleanGross >= 15000
    category = 'A'
CASE cleanGross >= 5000
    category = 'B'
CASE cleanGross >= 1000
    category = 'C'
    category = 'None'
Pause("cleanGross = %cleanGross%",category)
goto loop