Author Topic: Finding if a string is a number [Solved by Ipquarx]  (Read 2435 times)

How would I make a function that finds if a string is a number?
« Last Edit: September 14, 2012, 02:08:52 PM by jes00 »

I'd say use a for loop to check if any characters in the string are not numbers, . or -
Unless there's a default function for this already. I only know of the isInt function, which wouldn't help for floats

What do you mean by "a number"?

Checking if a number is an integer is as simple as going through every character in the string and making sure that it's in 0123456789.
Checking if a number is a float can be done by using some mathematical functions that would screw up when not receiving floats (and thus invalidate it) or by some state-based string checking.

What do you mean by "a number"?

Checking if a number is an integer is as simple as going through every character in the string and making sure that it's in 0123456789.
Checking if a number is a float can be done by using some mathematical functions that would screw up when not receiving floats (and thus invalidate it) or by some state-based string checking.
I want to check if it's an integer or a float.

I want to check if it's an integer or a float.

Haven't tested this extensively, but it should work in most cases.

if ( %value $= "0" || %value $= %value * 1 )

Checking if a number is an integer is as simple as going through every character in the string and making sure that it's in 0123456789.
What
u srs

isInt(%string)


Haven't tested this extensively, but it should work in most cases.

if ( %value $= "0" || %value $= %value * 1 )

You shouldn't need the %value $= "0" bit, because "0" $= "0" * 1 evaluates to true.

I think this is what most people use:

Code: [Select]
function isNumber(%val)
{
return (%val $= %val * 1);
}

That won't work with say, "1234567654321" though. Or in other words any number over 1 million not in scientific notation.

You shouldn't need the %value $= "0" bit, because "0" $= "0" * 1 evaluates to true.

I think this is what most people use:

Code: [Select]
function isNumber(%val)
{
return (%val $= %val * 1);
}
Code: [Select]
function isNumber(%val)
{
return %val $= (%val + 0);
}
function isInt(%val)
{
return %val $= (%val << 0);
}
I need that extra millisecond.
That won't work with say, "1234567654321" though. Or in other words any number over 1 million not in scientific notation.
This will at least.

I always just use if(%int+0 $= %int)

So how would I do it with a for statement?

So how would I do it with a for statement?

I'm not quite sure what you mean. There is no difference between calling a function or using an if statement outside or inside of a for statement.

I'm not quite sure what you mean. There is no difference between calling a function or using an if statement outside or inside of a for statement.
I want to know how to find if a string is a number using a for statement because that seems the only way that can handle over a million.

I want to know how to find if a string is a number using a for statement because that seems the only way that can handle over a million.

Code: [Select]
function isInt(%val)
{
return %val $= (%val << 0);
}