最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to check if a number is a float in javascript? - Stack Overflow

programmeradmin2浏览0评论

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 pose 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?

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 pose 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?

Share Improve this question asked Feb 6, 2020 at 12:38 Julio RodríguezJulio Rodríguez 4771 gold badge10 silver badges27 bronze badges 4
  • Does this answer your question? How do I check that a number is float or integer? – Marcelo The Mage Coder Commented Feb 6, 2020 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", ...]? – user5734311 Commented Feb 6, 2020 at 12:40
  • Does this answer your question? Validate decimal numbers in JavaScript - IsNumeric() – user5734311 Commented Feb 6, 2020 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 puters. 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 Commented Feb 6, 2020 at 12:45
Add a ment  | 

3 Answers 3

Reset to default 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.

This should Work

function isFloat(n){
    return Number(n) === n && n % 1 !== 0;
}

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")
   } 
 });

发布评论

评论列表(0)

  1. 暂无评论