how to tell what a variable is

G

greenflame

How can I tell if a variable. I want ot tell whether two variables are:

1. Both numbers. Just a number. same as the number object in
javascript.
2. Either one is a string containing a number. For exmaple a
string like "354" or "295.395".
3. Either are strings containing a fraction. For example a string
like "46/987".
4. Either is a "fraction". By "fraction" I mean that it is either
an array of two numbers or two number strings like in number 2.
The first being the top and the second being the bottom. For
example: [34,78] or ["34","57"].
 
R

RobG

greenflame said:
How can I tell if a variable. I want ot tell whether two variables are:

1. Both numbers. Just a number. same as the number object in
javascript.

To see if x is a number:

var x = 5;
if ( 'number' == typeof x ) // true

var x = '5';
if ( 'number' == typeof x ) // false

Note that if your values come from form text elements, they are strings
by default but may be converted to numbers.
2. Either one is a string containing a number. For exmaple a
string like "354" or "295.395".

I think you mean a string that can be converted to a number.

var x = '354';
if ( 'string' == typeof x ) // true

However, this test is a bit useless - a regular expression provides a
better solution:

if ( /^\d+[.]?[\d*]?$/.test(x) ) {

This will return true if x is one or more digits followed by an
optional '.' followed by more optional digits. Anything else will
return false. Note that this will rule out notation like 1.2e34 (which
is otherwise perfectly valid as a number in JavaScript).

Search the news group for examples of regular expression tests for
number formats, there are plenty of examples.
3. Either are strings containing a fraction. For example a string
like "46/987".

var x = '46/987';
if ( /\//.test(x) ) // true - string contains a '/' character

But you probably need to detect that there is only one '/' character
and that the stuff either side is a number.
4. Either is a "fraction". By "fraction" I mean that it is either
an array of two numbers or two number strings like in number 2.
The first being the top and the second being the bottom. For
example: [34,78] or ["34","57"].

var x = '34,78';
if ( /\,/.test(x) ) // true - string contains a ',' character

*Note*
Each of the above represent a trivial cases. There are other tests
that likely should be conducted, such as checking whether the variable
contains only digits, periods or commas, whether it contains multiple
instances of those characters, what is an OK value and what isn't, etc.

It is better to say how you are getting the input and what it is being
used for so that appropriate tests can be suggested.
 
M

Michael Winter

Just so you know, number objects are different from number values.

var v = 5, // value
o = new Number(5); // object

You can perform arithmetic with both as the number object will be
type-converted to a number first, but you cannot detect them in the same
way. Rob's suggestion will work for values, whilst

if(o && (Number == o.constructor))

will work for numbers. If you don't actually care about objects, then
never mind...

[snip]
if ( /^\d+[.]?[\d*]?$/.test(x) ) {

That's broken. For properly formatted decimal numbers,

/^[+-]?(0|[1-9]\d*)(\.\d*)?$/

is better.

[snip]

Presumably, the numerator and denominator should both be integers, and
the denominator should not be zero?

/^[+-]?(0|[1-9]\d*)\/[1-9]\d*$/
4. Either is a "fraction". By "fraction" I mean that it is either
an array of two numbers or two number strings like in number 2.
The first being the top and the second being the bottom. For
example: [34,78] or ["34","57"].

Though an array would be more convenient, an object would be more
appropriate. You could still use 0 and 1 as indices:

{0 : 34, 1 : 78}

or you could use named properties. If you used a constructor function,
then testing is very simple:

function Fraction(numerator, denominator) {
this.n = numerator; /* Or this[0] */
this.d = denominator;
}

var myFraction = new Fraction(34, 78);

if(myFraction && (Fraction == myFraction.constructor)) {
/* Is a Fraction instance */
}

You could add checks into the constructor to ensure that the arguments
are numbers, or number-like strings if you wanted to.

[snip]

Mike
 
G

greenflame

Hmmm... thanks for the help. It seems I should learn about regular
expressions. So what are they? Where can I learn all about them?
 
G

greenflame

Um.... sorry for posting twice but I for got to say somenting... Rob
G.. your last way to check if a variable is of the format [324,72] or
["345","36"] doesnt actually test if it is of this format or atleast it
doesnt seem to they way you made the code. I havent tryed it but it
seems like it would only detect if the contents are of the form
"543,876".
 
G

greenflame

Again sorry for posting what is now three straight posts. I was
wondering like how to tell if a variable is a string and then convert
to number. for example convert :

1. "2" to 2
2. "1.653" to 1.653
3. "1.325e23" to 1.325e23
4. "4.24e-4" to 4.24e-4

I know this was partially covered above. Also I am probably having my
question sort of answered twice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,839
Latest member
MartinaBur

Latest Threads

Top