{"id":570,"date":"2022-08-30T15:03:33","date_gmt":"2022-08-30T15:03:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-check-if-a-number-is-float-or-integer-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:33","modified_gmt":"2022-08-30T15:03:33","slug":"how-to-check-if-a-number-is-float-or-integer-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-check-if-a-number-is-float-or-integer-collection-of-common-programming-errors\/","title":{"rendered":"How to check if a number is float or integer?-Collection of common programming errors"},"content":{"rendered":"<p>how to find if a number is float or integer?<\/p>\n<pre><code>1.25 --&gt; float  \n1 --&gt; integer  \n0 --&gt; integer  \n0.25 --&gt; float\n<\/code><\/pre>\n<ol>\n<li>\n<p>check a remainder when dividing by 1:<\/p>\n<pre><code>function isInt(n) {\n   return n % 1 === 0;\n}\n<\/code><\/pre>\n<p>If you don&#8217;t know that the argument is a number-<\/p>\n<pre><code>function isInt(n) {\n   return typeof n === 'number' &amp;&amp; n % 1 == 0;\n}\n<\/code><\/pre>\n<p>If you also want to include examples such as 1E308 is a float, and not an integer:<\/p>\n<pre><code>function isInt(n) {\n   return typeof n === 'number' &amp;&amp; parseFloat(n) == parseInt(n, 10) &amp;&amp; !isNaN(n);\n} \/\/ 6 characters\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Try this.<\/p>\n<pre><code>function isFloat (n) {\n  return n===+n &amp;&amp; n!==(n|0);\n}\n\nfunction isInteger (n) {\n  return n===+n &amp;&amp; n===(n|0);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Here are efficient functions that check if the value is a number or can be <em>safely converted to<\/em> a number:<\/p>\n<pre><code>function isNumber(value) {\n    if ((undefined === value) || (null === value)) {\n        return false;\n    }\n    if (typeof value == 'number') {\n        return true;\n    }\n    return !isNaN(value - 0);\n}\n<\/code><\/pre>\n<p>And for integers (would return false if the value is a float):<\/p>\n<pre><code>function isInteger(value) {\n    if ((undefined === value) || (null === value)) {\n        return false;\n    }\n    return value % 1 == 0;\n}\n<\/code><\/pre>\n<p>The efficiency here is that parseInt (or parseNumber) are avoided when the value already is a number. Both parsing functions <em>always<\/em> convert to string first and then attempt to parse that string, which would be a waste if the value already is a number.<\/p>\n<p>Thank you to the other posts here for providing further ideas for optimization!<\/p>\n<\/li>\n<li>\n<p>You can use a simple regular expression:<\/p>\n<pre><code>function isInt(value)\n{\n    var er = \/^[0-9]+$\/;\n\n    return ( er.test(value) ) ? true : false;\n}\n<\/code><\/pre>\n<p>Or you can use the below functions too, according your needs. They are developed by the PHPJS Project.<\/p>\n<p><code>is_int()<\/code> =&gt; Check if the variable type is Integer and if its content is Integer<\/p>\n<p><code>is_float()<\/code> =&gt; Check if the variable type is Float and if its content is Integer<\/p>\n<p><code>ctype_digit()<\/code> =&gt; Check if the variable type is String and if its content has only decimal digits<\/p>\n<\/li>\n<li>\n<p>Why not something like this:<\/p>\n<pre><code>var isInt = function(n) { return parseInt(n) === n };\n<\/code><\/pre>\n<\/li>\n<li>\n<p>It really depends on what you want to achieve. If you want to &#8220;emulate&#8221; strongly typed languages then I suggest you not trying. As others mentioned all numbers have the same representation (the same type).<\/p>\n<p>Using something like <em>Claudiu<\/em> provided:<\/p>\n<p><code>isInteger( 1.0 )<\/code> -&gt; true<\/p>\n<p>which looks fine for common sense, but in something like C you would get <code>false<\/code><\/p>\n<\/li>\n<li>\n<p>As others mentioned, you only have doubles in JS. So how do you define a number being an integer? Just check if the rounded number is equal to itself:<\/p>\n<pre><code>function isInteger(f) {\n    return typeof(f)===\"number\" &amp;&amp; Math.round(f) == f;\n}\nfunction isFloat(f) { return typeof(f)===\"number\" &amp;&amp; !isInteger(f); }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>THIS IS FINAL CODE FOR CHECK BOTH INT AND FLOAT<\/p>\n<pre><code>function isInt(n) { \n   if(typeof n == 'number' &amp;&amp; Math.Round(n) % 1 == 0) {\n       return true;\n   } else {\n       return false;\n   }\n} \n<\/code><\/pre>\n<p>OR<\/p>\n<pre><code>function isInt(n) {   \n   return typeof n == 'number' &amp;&amp; Math.Round(n) % 1 == 0;   \n}   \n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>function isInteger(n) {\n   return ((typeof n==='number')&amp;&amp;(n%1===0));\n}\n\nfunction isFloat(n) {\n   return ((typeof n==='number')&amp;&amp;(n%1!==0));\n}\n\nfunction isNumber(n) {\n   return (typeof n==='number');\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>function isInt(n) \n{\n    return n != \"\" &amp;&amp; !isNaN(n) &amp;&amp; Math.round(n) == n;\n}\nfunction isFloat(n){\n    return n != \"\" &amp;&amp; !isNaN(n) &amp;&amp; Math.round(n) != n;\n}\n<\/code><\/pre>\n<p>works for all cases.<\/p>\n<\/li>\n<li>\n<p>Another method is:<\/p>\n<pre><code>    function isFloat(float) {\n        return \/\\.\/.test(float.toString());\n    }\n<\/code><\/pre>\n<p>Might not be as efficient as the others but another method all the same.<\/p>\n<\/li>\n<li>\n<pre><code>!!(24%1) \/\/ false\n!!(24.2%1) \/\/ true\n<\/code><\/pre>\n<\/li>\n<li>\n<p>For integers I use this<\/p>\n<pre><code>function integer_or_null(value) {\n    if ((undefined === value) || (null === value)) {\n        return null;\n    }\n    if(value % 1 != 0) {\n        return null;\n    }\n    return value;\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>It really doesn&#8217;t have to be so complicated. The numeric value of an integer&#8217;s parseFloat() and parseInt() equivalents will be the same. Thus you can do like so:<\/p>\n<pre><code>function isInt(value){ \n    return (parseFloat(value) == parseInt(value)) &amp;&amp; !isNaN(value);\n}\n<\/code><\/pre>\n<p>Then<\/p>\n<pre><code>if (isInt(x)) \/\/ do work\n<\/code><\/pre>\n<p>This will also allow for string checks and thus is not strict. If want a strong type solution (aka, wont work with strings):<\/p>\n<pre><code>function is_int(value){ return !isNaN(parseInt(value * 1) }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>In java script all the numbers are <code>internally 64 bit floating point<\/code>, same as double in java. There are no diffrent types in javascript, all are represented by type <code>number<\/code>. Hence you wil l not be able make a <code>instanceof<\/code> check. However u can use the above solutions given to find out if it is a fractional number. designers of java script felt with a single type they can avoid numerous type cast errors.<\/p>\n<\/li>\n<li>\n<p><code>float<\/code> values consists of <code>.<\/code>, so here we can use <code>indexOf<\/code> method.<\/p>\n<pre><code>function isInt(number){\n    if(number.toString().indexOf(\".\") &gt; -1)\n        return false; \/\/ it is float\n    else \n        return true; \/\/it is integer\n}  \n<\/code><\/pre>\n<p>The above code is only applicable if the given variable is <code>number<\/code>. if you are not sure about the type of variable , then do the following before the above code:<\/p>\n<pre><code>\/\/as suggested by @kennebec\nfunction isNumber(number){\n    return typeof n === 'number'\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Any Float number with a zero decimal part (e.g. 1.0, 12.00, 0.0) are implicitly cast to Integer, so it is not possible to check if they are Float or not.<\/p>\n<\/li>\n<li>\n<p>Here&#8217;s what I use for integers:<\/p>\n<pre><code>Math.ceil(parseFloat(val)) === val\n<\/code><\/pre>\n<p>Short, nice \ud83d\ude42 Works all the time. This is what David Flanagan suggests if I&#8217;m not mistaken.<\/p>\n<\/li>\n<li>\n<pre><code>parseInt(yourNumber)=== parseFloat(yourNumber)\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:01:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>how to find if a number is float or integer? 1.25 &#8211;&gt; float 1 &#8211;&gt; integer 0 &#8211;&gt; integer 0.25 &#8211;&gt; float check a remainder when dividing by 1: function isInt(n) { return n % 1 === 0; } If you don&#8217;t know that the argument is a number- function isInt(n) { return typeof n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}