How can I check if a string is a float?

前端 未结 14 1815
清歌不尽
清歌不尽 2020-12-23 20:21

I am passing in a parameter called value. I\'d like to know if value is a float. So far, I have the following:

if (!isNaN(value))
{
    alert(\'this is a num         


        
14条回答
  •  星月不相逢
    2020-12-23 20:40

    If you need to check if value is int or float:

    function isFloatOrInt(n) {
        return !isNaN(n) && n.toString().match(/^-?\d*(\.\d+)?$/);
    }
    

提交回复
热议问题