-1

Is there a method or something alike to check wether a number is a float?

I'm receiving string arrays from server, and I need to perform some operations on the elements that compose them depending their type. I tried this to figure out the type of the elements:

 let a = [4, 2.3, "SO2", 4, "O2", 3.4, 4.5, "CO2", 5.6, 3.4, 2];

itExists = true;

 a.forEach((e,i,a)=>{
   if(true){
     if(typeof e  == 'number'){        
            if ( e % 1 != 0) {
               console.log(e + " :float")
            }else{
                console.log(e + " :number")
             }
       }else{
         console.log(e + " :letter")
       }
   }else{
     console.log("Does not exist")
   } 
 });

However, this doesn't work, as they are strings. Any idea?

Julio Rodríguez
  • 447
  • 1
  • 8
  • 23
  • Does this answer your question? [How do I check that a number is float or integer?](https://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-float-or-integer) – Marcelo The Mage Coder Feb 06 '20 at 12:39
  • Your code works exactly as it should as far as I can see, is the actual (parsed) server response something like `[ "4", "2.3", ...]`? –  Feb 06 '20 at 12:40
  • Does this answer your question? [Validate decimal numbers in JavaScript - IsNumeric()](https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) –  Feb 06 '20 at 12:42
  • There is no such thing as a "float number". Float (short of *"floating point"*) is a way to represent the real numbers in computers. In JavaScript, all numbers (integer or not) are stored using the floating point format. Is your question about how to tell apart the integer numbers from the numbers that also have fractional part? (e.g. `4` is an integer, `2.3` is not). – axiac Feb 06 '20 at 12:45

3 Answers3

2

This should Work

function isFloat(n){
    return Number(n) === n && n % 1 !== 0;
}
vinayak shahdeo
  • 1,348
  • 2
  • 11
  • 21
2

To convert a string to a Number you can use the Number constructor:

const n = Number('my string') // NaN ("not a number")

To check to see if the conversion was a valid number:

Number.isNaN(n)

Note: Internet Explorer does not support Number.isNaN and for that you can use the older isNaN. More here and here.

To check if a Number is an integer, there are two built-in methods:

  1. Number.isInteger(), and
  2. Number.isSafeInteger()

Further discussion can be found here.

let a = [4, 2.3, "SO2", 4, "O2", 3.4, 4.5, "CO2", 5.6, 3.4, 2];

a.forEach((e)=>{
    const n = Number(e)

    if (Number.isNaN(n)) {
        console.log(e + " :letter")
        return
    }

    if (Number.isInteger(n)) {
        console.log(e + " :integer")
        return
    }

    console.log(e + " :non-integer number")
})

Note

“Floating point” is the name of the technology used to represent the numbers in binary. This is why I changed the terminology from "float" to "non-integer number" in the code above.

Some numeric values in JavaScript contain non-numeric characters. eg.

Infinity
0x3d // hexadecimal
3n // BigInt
0b101011 // Binary
1e2 // times ten to the power of...

There are now two kinds of number in JavaScript: Number and BigInt.

Number is an implementation of the IEEE 754 32-bit floating point number specification.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
1

Replace you condition as follows

(e % 1 != 0)//replace this
(e - parseInt(e)) // to this

parseInt() converts floats into integers Final code will look like as follows

let a = [4, 2.3, "SO2", 4, "O2", 3.4, 4.5, "CO2", 5.6, 3.4, 2];

itExists = true;

 a.forEach((e,i,a)=>{
   if(true){
     if(typeof e  == 'number'){        
            if ( e-parseInt(e)) {
               console.log(e + " :float")
            }else{
                console.log(e + " :number")
             }
       }else{
         console.log(e + " :letter")
       }
   }else{
     console.log("Does not exist")
   } 
 });
Nayab
  • 21
  • 1