force string to number problem

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

force string to number problem

Post by rblack » Wed Mar 21, 2012 2:57 pm

hi there!

I have many situations, where Macro Sheduler doesnt recognize the number as a number, and handles it as string. It makes difficult to calculate complex expressions and hangs the script. Here is the simpliest code, that makes an error:

Code: Select all

let>number=1235,8
let>RAW=%number%-1,20
let>number={Trunc(%RAW%)}
MessageModal>%number%
How to force Macro Sheduler to convert string to number? I tried to write the variable on disc, and read it from files, but that does not always work.

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Mar 22, 2012 5:21 pm

Code: Select all

let>number=1235.8
let>RAW=%number%-1.20
let>number={Trunc(%RAW%)}
MessageModal>%number%
Works perfectly for me.

I simply changed your commas , to decimal . points.

Of course if you are using a language where the decimal separator is a comma then you would have to account for that.

Marcus may offer you more info on that subject.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Mar 22, 2012 5:26 pm

If the problem is the decimal place holder.

In the past I did it this way (with help from Marcus):

Code: Select all

//Determine the decimal placeholder
Let>num=0.1
LibFunc>kernel32,GetNumberFormatA,r,0,0,str:num,0,buf,255
MidStr>r_5,2,1,DecimalPlaceHolder
Then from then in order to do math you use %DecimalPlaceHolder% in place of (obviously) your decimal place holder:

So twelve point five (12.5) would be written as 12%DecimalPlaceHolder%5
Phil Pendlebury - Linktree

User avatar
Marcus Tettmar
Site Admin
Posts: 7393
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 22, 2012 6:12 pm

You can also get/set the decimal and thousand seperators. See systen variables in help file:

DECIMAL_SEPARATOR
Sets/retrieves the character used as the decimal separator in floating point/currency numbers. Default is "."

THOUSAND_SEPARATOR
Sets/retrieves the character used as the thousand separator in numbers. Default is ","
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Fri Mar 23, 2012 1:32 pm

Ahh that's new - Great stuff - I did say "in the past" :)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Fri Mar 23, 2012 1:33 pm

So the script would become:

Code: Select all

let>number=1235%DECIMAL_SEPARATOR %8
let>RAW=%number%-1%DECIMAL_SEPARATOR %20
let>number={Trunc(%RAW%)}
MessageModal>%number%
Phil Pendlebury - Linktree

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Fri Mar 23, 2012 2:21 pm

i just entered those lines as an example. I read the data from website, and the numbers are comma separated.

I could replace the comma in the read variable using StringReplace, but that doesnt work well with the comma sign {,} . It likes to hang the script when repeated many times in a single script.

Isnt there any "string 2 int" function, to use it securely in such situations? It is really frustrating to do so many tricks in many scripts.

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Thu Mar 29, 2012 5:01 pm

i m pretty new to MS but i've run through the same problem using stringreplace to replace comma with a different values.

what i came up with:

let>comma=,
let>replace=.
stringreplace>%source%,%comma%,%replace%,source

this should assign the , and change it to . in your source string and put it back into the source string.

Other way would likely be vbscript function send the value use its cint or cdbl and return the value back to MS.

User avatar
Marcus Tettmar
Site Admin
Posts: 7393
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 29, 2012 5:07 pm

rblack wrote:i just entered those lines as an example. I read the data from website, and the numbers are comma separated.

I could replace the comma in the read variable using StringReplace, but that doesnt work well with the comma sign {,} . It likes to hang the script when repeated many times in a single script.

Isnt there any "string 2 int" function, to use it securely in such situations? It is really frustrating to do so many tricks in many scripts.
Which version of Macro Scheduler are you using? You shouldn't need to do lots of tricks.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Fri Mar 30, 2012 1:26 pm

im using Macro Scheduler 11.1.17.

Im afraid to upgrade, because currently i have about 20000 lines of code working 24/7 on a dedicated server. Cant allow to have any compatibilty problems.


the solution

Code: Select all

let>comma=,
let>replace=.
stringreplace>%source%,%comma%,%replace%,source 
is good, but for simple scripts. With complex ones, it also generates some problems in called functions/SRTs and hangs the macro.


I tried a workaround with separate command, but it also doesnt work well. Maybe there is an VBSCRIPT function to round a variable to integer?

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Fri Mar 30, 2012 1:48 pm

nevertheless after a lot of tests, the solution:

Code: Select all

   let>delim=,
   Separate>%number%,%delim%,newnum
   let>integer=%newnum_1%
is the most stable.

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Tue Apr 22, 2014 7:40 pm

hi, i had a problem and searching the Internet I have found my own reply with the solution ;) But the solution is only partial - it limits the float number to INT.

Here is the code I Have problem with:

Code: Select all

        let>amount=12,99
        let>lesser=1
        let>larger=100
        //there is no greater or equal sign in my MS version
        MessageModal> %amount%>%lesser% %amount%<larger>{(%amount% > %lesser%) AND (%amount% <larger>amount is BETWEEN
         else
          if>{(%amount% = %lesser%) OR (%amount% = %larger%)}
           MessageModal>amount may be EQUAL
          endif
        endif
        if>%amount%>%lesser%
          MessageModal>BIGGER than %lesser%
        endif
        if>%amount%<larger>LESSER THAN %larger%
        endif

When i Run this, I get an answer that 12,99 is only BIGGER then 1 and not LESSER THAN 100. I tried to separate 12 and 99, then divide 99 by 100 in a different variable. It displays 12,99, but it doesnt fit the equasion above. How can I solve this problem? My Macro Scheduller version is still 11.

User avatar
Marcus Tettmar
Site Admin
Posts: 7393
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Apr 22, 2014 7:50 pm

Complex Expressions (the bit between { and }) doesn't understand numbers with commas in them. Commas are reserved for parameter delimiliters.

Outside of complex expressions you CAN use commas but only if your regional settings allow it.

Usually as a rule of thumb, in any programming language it is best to avoid commas and use ".".

If you want to DISPLAY commas to the user by all means format the string afterwards. But generally speaking the "," makes it a string not a number.

Anyway, if your regional settings allow it this should work:

Code: Select all

Let>DECIMAL_SEPARATOR=,
let>amount=12,99
let>lesser=1
let>larger=100

if>amount>lesser
  If>amount<larger
     MessageModal>%amount% is between %lesser% and %larger%
  Endif
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts