How do I check that a number is float or integer?

2018-12-31 03:48发布

How to find that a number is float or integer?

1.25 --> float  
1 --> integer  
0 --> integer  
0.25 --> float

30条回答
千与千寻千般痛.
2楼-- · 2018-12-31 04:38

It really depends on what you want to achieve. If you want to "emulate" strongly typed languages then I suggest you not trying. As others mentioned all numbers have the same representation (the same type).

Using something like Claudiu provided:

isInteger( 1.0 ) -> true

which looks fine for common sense, but in something like C you would get false

查看更多
不流泪的眼
3楼-- · 2018-12-31 04:38

For integers I use this

function integer_or_null(value) {
    if ((undefined === value) || (null === value)) {
        return null;
    }
    if(value % 1 != 0) {
        return null;
    }
    return value;
}
查看更多
美炸的是我
4楼-- · 2018-12-31 04:40

Condtion for floating validation :

if (lnk.value == +lnk.value && lnk.value != (lnk.value | 0)) 

Condtion for Integer validation :

if (lnk.value == +lnk.value && lnk.value == (lnk.value | 0)) 

Hope this might be helpful.

查看更多
零度萤火
5楼-- · 2018-12-31 04:41

It's simple as:

if( n === parseInt(n) ) ...

Try this in console:

x=1;
x===parseInt(x); // true
x="1";
x===parseInt(x); // false
x=1.1;
x===parseInt(x); // false, obviously

// BUT!

x=1.0;
x===parseInt(x); // true, because 1.0 is NOT a float!

This confuses a lot of people. Whenever something is .0, it's not a float anymore. It's an integer. Or you can just call it "a numeric thing" for there is no strict distinction like back then in C. Good old times.

So basically, all you can do is check for integer accepting the fact that 1.000 is an integer.

Interesting side note

There was a comment about huge numbers. Huge numbers mean NO problem for this approach; whenever parseInt is unable to handle the number (for it's too big) it will return something else than the actual value so the test will return FALSE. This is a good thing because if you consider something a "number" you normally expect JS to be able to calculate with it - so yes, numbers are limited and parseInt will take this into consideration, to put it this way.

Try this:

<script>

var a = 99999999999999999999;
var b = 999999999999999999999; // just one more 9 will kill the show!
var aIsInteger = (a===parseInt(a))?"a is ok":"a fails";
var bIsInteger = (b===parseInt(b))?"b is ok":"b fails";
alert(aIsInteger+"; "+bIsInteger);

</script>

In my browser (IE8) this returns "a is ok; b fails" which is exactly because of the huge number in b. The limit may vary but I guess 20 digits "ought to be enough for anybody", to quote a classical :)

查看更多
梦醉为红颜
6楼-- · 2018-12-31 04:43

THIS IS FINAL CODE FOR CHECK BOTH INT AND FLOAT

function isInt(n) { 
   if(typeof n == 'number' && Math.Round(n) % 1 == 0) {
       return true;
   } else {
       return false;
   }
} 

OR

function isInt(n) {   
   return typeof n == 'number' && Math.Round(n) % 1 == 0;   
}   
查看更多
与风俱净
7楼-- · 2018-12-31 04:45
var isInt = function (n) { return n === (n | 0); };

Haven't had a case where this didn't do the job.

查看更多
登录 后发表回答