parseInt function.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

parseInt function.

ved sharma
I want to test if a string is a number or a character and I am doing it by using parseInt() macro function. The output I get for the following macro is that a is a number. Can someone tell me where am I making the mistake?


a = "text";
print(parseInt(a));
if(parseInt(a) == NaN)
        print("a is not a number");
else
        print("a is a number ->"+a);


Thanks,
Ved
Reply | Threaded
Open this post in threaded view
|

Re: parseInt function.

Gabriel Landini
On Tuesday 22 Nov 2011 20:14:57 you wrote:
> I want to test if a string is a number or a character and I am doing it by
> using parseInt() macro function. The output I get for the following macro
> is that a is a number. Can someone tell me where am I making the mistake?
 
What about using: isNaN(n)

From http://imagej.nih.gov/ij/developer/macro/functions.html

isNaN(n)
Returns true if the value of the number n is NaN (Not-a-Number). A common way
to create a NaN is to divide zero by zero. Comparison with a NaN always
returns false so "if (n!=n)" is equilvalent to (isNaN(n))". Note that the
numeric constant NaN is predefined in the macro language. The NaNs macro
demonstrates how to remove NaNs from an image.

Cheers
G
Reply | Threaded
Open this post in threaded view
|

Re: parseInt function.

BenTupper
In reply to this post by ved sharma
On Nov 22, 2011, at 3:14 PM, Ved Sharma wrote:

> I want to test if a string is a number or a character and I am doing it by using parseInt() macro function. The output I get for the following macro is that a is a number. Can someone tell me where am I making the mistake?
>
>
> a = "text";
> print(parseInt(a));
> if(parseInt(a) == NaN)
> print("a is not a number");
> else
> print("a is a number ->"+a);
>
>

Hi,

How about inverting the logic of isNaN() ?

function isNumber(x){
        return(!isNaN(x));
}


print("boo -> " + isNumber("boo"));
print("3.14 -> " + isNumber("3.14"));
print("2.18e-2 -> " + isNumber("2.18e-2"));


Cheers,
Ben
Reply | Threaded
Open this post in threaded view
|

Re: parseInt function.

ved sharma
In reply to this post by ved sharma
Hi Gabriel and Ben,

Thanks for your suggestion. Though I found some inconsistency in isNaN() function.

Following code run fine:
a = "text";
print(isNaN(a));

But the following code:
print(isNaN("text"));

gives a "number or numeric function expected" error.

Just wondering why?

Ved

On Tue, 22 Nov 2011 15:35:23 -0500, Ben Tupper <[hidden email]> wrote:

>On Nov 22, 2011, at 3:14 PM, Ved Sharma wrote:
>
>> I want to test if a string is a number or a character and I am doing it by using parseInt() macro function. The output I get for the following macro is that a is a number. Can someone tell me where am I making the mistake?
>>
>>
>> a = "text";
>> print(parseInt(a));
>> if(parseInt(a) == NaN)
>> print("a is not a number");
>> else
>> print("a is a number ->"+a);
>>
>>
>
>Hi,
>
>How about inverting the logic of isNaN() ?
>
>function isNumber(x){
> return(!isNaN(x));
>}
>
>
>print("boo -> " + isNumber("boo"));
>print("3.14 -> " + isNumber("3.14"));
>print("2.18e-2 -> " + isNumber("2.18e-2"));
>
>
>Cheers,
>Ben