WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: 1bosborn on October 03, 2014, 10:34:55 AM

Title: Number not evaluating as expected
Post by: 1bosborn on October 03, 2014, 10:34:55 AM
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
   EndIf
   If gross >= '1000'                      ;this cam back true (expected)
      If gross <= '4999' Then category = 'C'  ;this came back true (unexpected)
   EndIf
I have included comments as to how each evaluated
Title: Re: Number not evaluating as expected
Post by: JTaylor on October 03, 2014, 10:56:22 AM
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.

Jim
Title: Re: Number not evaluating as expected
Post by: 1bosborn on October 03, 2014, 11:19:43 AM
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?
Title: Re: Number not evaluating as expected
Post by: td on October 03, 2014, 01:03:27 PM
User StrClean

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

to remove comas and get rid of the single quotes around your numeric literals. 
Title: Re: Number not evaluating as expected
Post by: JTaylor on October 03, 2014, 06:45:57 PM
Commas will not work in "numbers".

Jim
Title: Re: Number not evaluating as expected
Post by: snowsnowsnow on October 03, 2014, 11:18:17 PM
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"

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