How do I check whether a string is a number?

Get help using Construct 2

Post » Fri Jul 25, 2014 1:51 pm

Quick question: is there a function that return true when a given string is a number?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Fri Jul 25, 2014 2:06 pm

int("string") > 0 will work other than for zero. Otherwise you could use a regular expression.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Fri Jul 25, 2014 2:17 pm

For additional precision you could use

"string" = "0"
or
int("string") != 0.
B
11
S
3
G
1
Posts: 98
Reputation: 1,062

Post » Fri Jul 25, 2014 2:47 pm

Thanks guys :)
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Fri Jul 25, 2014 3:11 pm

There is a function that returns true when a given string is NOT a number. It's not wrapped in any System expression so you'll have to use the Browser.ExecJS expression.

Code: Select all
Browser.ExecJS("!isNaN(""" & yourVar & """);")


This will return 1 if yourVar is a valid javascript number.
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Sat Sep 06, 2014 4:44 pm

int(x) got one problem. If x < 1 (ie. 0.5), it will return 0, which is also the value to indicate that x is not a number. To remedy this, use float(x) instead.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 30 guests