[SOLVED] Convert string data to number type - Forum

Forum Navigation
You need to log in to create posts and topics.

[SOLVED] Convert string data to number type

Hi everyone,

It's few days that my program faced some stupid wired problem which made me crazy...
I have no idea whats happening.

I have if code there:

SetVar [var1] 7
SetVar [var2] 56

If [var1] >= [var2]
     jsAlert "[var1] is greater than [var2]"
EndIf

And alert says:
7 is greater than 56

that "SetVar [var1] 7" and "SetVar [var2] 56" are not exactly same as my code, i get data from arrays:

SetVar [var1] [players(0)('block')]
SetVar [var2] [games(0)('mapmax')]

above lines are exact code of my program.

I'm not sure you can check it in new clean project, Maybe something wrong somewhere else and make this part go crazy...

@noyzen

If I understand you correctly ...

"SetVar [var1] 7" and "SetVar [var2] 56" are not exactly same as my code, i get data from arrays:

SetVar [var1] [players(0)('block')]
SetVar [var2] [games(0)('mapmax')]

... the alert message was the result of you deploying ...

SetVar [var1] [players(0)('block')]
SetVar [var2] [games(0)('mapmax')]

If [var1] >= [var2]
jsAlert "[var1] is greater than [var2]"
EndIf

... if so, this could be because [players(0)('block')] and [games(0)('mapmax')] are string (not numeric) variables ... hence [var1] and [var2] are treated as strings (text).

When you compare text variables, the comparison is done from left to right ... same as if you were comparing Q with ABC) ... since 7 is greater than 5, the comparison returns true.

Try ...

SetVar [var1Str] [players(0)('block')]
SetVar [var2Str] [games(0)('mapmax')]

Math "1*[var1Str]" "0" "[var1]"
Math "1*[var2Str]" "0" "[var2]"

If [var1] >= [var2]
   jsAlert "[var1] is greater than [var2]"
EndIf

 

noyzen has reacted to this post.
noyzen

@gaev Man you are genius i love you!

Yes i had checked and knew both variables are strings using vartype command, but i failed to find a way to convert them into number type!

I tried your method and now code is working!

I used this data types in many parts of my app, before i go and change them all i need to know if this is best way to convert strings to numbers or maybe @luishp can make a command for that. (Convert String to number)

Thanks a lot!!!

 

@noyzen , you have ToNumber action for that

 

In VisualNEO Web?

Can you address me function group name or type me an example please?

ToNumber "3" [num]

 

Uploaded files:
  • You need to login to have access to uploads.
noyzen has reacted to this post.
noyzen

Thanks!

How noob i was... :D

YASIN has reacted to this post.
YASIN

So, I found the easy and correct way to do the job:

ToNumber "[players(0)('block')]" [var1]

(no need setvar command, that line is enouth)

luishp has reacted to this post.
luishp

@emmanuel-fernandez

Thanks for pointing to the ToNumber command ... I knew there was something like that available, but 30 minutes of searching the (online) Help file did not reveal it ... perhaps because the Help file is not uptodate.

noyzen has reacted to this post.
noyzen

Yea same here, i failed to find it bacause i searched help file! :)

yes @gaev and @noyzen is not in the online help and that's why it's hard to find unless you look and search at actions.

The same thing happened to me a year ago that I couldn't find it and I ended up using parseInt from javascript until @luishp reminded me of it.

noyzen has reacted to this post.
noyzen

@gaev @noyzen @emmanuel-fernandez I'm very sorry. I promise to review all documentation core commands and include them as soon as possible.
Hopefully, thanks to @susan, all plugin commands are listed and they are really easy to include now.
Sorry again!

Regards.

noyzen and emo have reacted to this post.
noyzenemo