[thelist] JavaScript : How to determine if float or integer

Peter Barrett Peter.Barrett at corel.com
Tue Jan 28 09:54:18 CST 2003


The issue here, as I see it is 3.000 mod 1 is going to tell you it's an
integer... So I guess my question is, are you worried about the
presentation format, or the mathematical value? If it's the presentation
format (dealing with display of money, for example), I think you hit it
on the head originally with 'scan for a .', since javascript won't
distinguish between them for you (a regexp, or an num.indexOf(".") != -1
would be fine).

If you just want to know if the number is mathematically an integer then

var a = parseInt(num);
var b = parseFloat(num);

return (a == b);

Should do the trick, I think....

~pete

-----Original Message-----
From: Rob Whitener [mailto:rwhitener at DesignOptions.com]
Sent: Tuesday, January 28, 2003 10:49 AM
To: 'thelist at lists.evolt.org'
Subject: RE: [thelist] JavaScript : How to determine if float or integer


I don't know if using (mynumb mod 1) will tell you if the number is an
integer.  There shouldn't ever be a remainder when something is divided
by 1, so the modulus will always equal 0.  Try dividing something like
.0256 by 1 and see if there is a remainder.

Rob

-----Original Message-----
From: Dawn Gabriel [mailto:dawn at ergito.com]
Sent: Tuesday, January 28, 2003 10:38 AM
To: thelist at lists.evolt.org
Subject: Re: [thelist] JavaScript : How to determine if float or integer


Slightly quicker way to do it:

function isInt(myNum) {
         // get the modulus: if it's 0, then it's an integer
         var myMod = myNum % 1;

         if (myMod == 0) {
                 return true;
         } else {
                 return false;
         }
}

At 07:00 PM 1/28/2003 +0500, you wrote:
>From: "Markus Staas"
>To: <thelist at lists.evolt.org>
>Sent: January 28, 2003, Tuesday 4:00 PM
>Subject: [thelist] JavaScript : How to determine if float or integer
>
>
> > Do you know any other way than scanning for the point in the whole
> > variable?
>
>Use indexOf() method.
>
>E.g.
>
><script language="JavaScript">
>
>var numbContainer
>var whatType
>var notNumber
>
>numbContainer=3.141                 // assigning a number to the
variable
>
>notNumber="a"+numbContainer   // this step will convert the number
>                                                     // into string
>without effecting the original number
>
>whatType=notNumber.indexOf(".")
>
>alert(whatType)    // a posititive integer (2) is returned whcih tells
the
>                             // position of point in the number
>
>numbContainer=786             // assigning another number to the
variable
>
>notNumber="a"+numbContainer   // this step will convert the number
>                                                    // into string
>without effecting the original number
>
>whatType=notNumber.indexOf(".")
>
>alert(whatType)    // as point will not be found therefore -1 will be
returned
>
></script>
>
>Hope this helps!
>
>Syed Zeeshan Haider.
>http://syedzeeshanhaider.faithweb.com/
>
>--
>* * Please support the community that supports you.  * *
>http://evolt.org/help_support_evolt/
>
>For unsubscribe and other options, including the Tip Harvester and
>archives of thelist go to: http://lists.evolt.org Workers of the Web,
>evolt !

--
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and
archives of thelist go to: http://lists.evolt.org Workers of the Web,
evolt !
--
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester
and archives of thelist go to: http://lists.evolt.org
Workers of the Web, evolt !



More information about the thelist mailing list