How can I check if a string is a float?

2019-02-01 05:29发布

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 numeric value but not sure if it is a float.');
}

How do I go one step further and convert the string to something that can evaluate to a float?

15条回答
\"骚年 ilove
2楼-- · 2019-02-01 06:05

I wrote functions which accept strings, integers and floats

function isInt(x) {
    return !isNaN(x) && eval(x).toString().length == parseInt(eval(x)).toString().length
}

function isFloat(x) {
    return !isNaN(x) && !isInt(eval(x)) && x.toString().length > 0
}

example ouptuts:

console.log(isFloat('0.2'))  // true
console.log(isFloat(0.2))    // true
console.log(isFloat('.2'))   // true
console.log(isFloat('-.2'))  // true
console.log(isFloat(-'.2'))  // true
console.log(isFloat(-.2))    // true
console.log(isFloat('u.2'))  // false
console.log(isFloat('2'))    // false
console.log(isFloat('0.2u')) // false

console.log(isInt('187'))  // true
console.log(isInt(187))    // true
console.log(isInt('1.2'))  // false
console.log(isInt('-2'))   // true
console.log(isInt(-'1'))   // true
console.log(isInt('10e1')) // true
console.log(isInt(10e1))   // true
查看更多
forever°为你锁心
3楼-- · 2019-02-01 06:06

Like this:

if (!isNaN(value) && value.toString().indexOf('.') != -1)
{
    alert('this is a numeric value and I\'m sure it is a float.');
}​
查看更多
\"骚年 ilove
4楼-- · 2019-02-01 06:06
Number.prototype.isFloat = function() {
    return (this % 1 != 0);
}

Then you can

var floatValue = parseFloat("2.13");
var nonFloatValue = parseFloat("11");

console.log(floatValue.isFloat()); // will output true
console.log(nonFloatValue.isFloat()); // will output false

Values like 2.00 cannot really be considered float in JS, or rather every number is a float in JS.


EDIT: Forgot to mention that extending the prototypes of built-in types is considered a bad parctice (for a good reason) and its use is discouraged in production environments. You could still implement this check as a plain function

查看更多
冷血范
5楼-- · 2019-02-01 06:06

use this jquery code

    <script>
    $(document).ready(function () {
        $(".allow_only_float").keypress(function (e) {

            if (e.which != 46 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                e.preventDefault();//return false;
            }
            var avg = $('#<%=txtAverage.ClientID%>').val();
            var idx = avg.indexOf(".");
            if (idx > -1 && e.which == 46) {
                e.preventDefault();//return false;
            }
        });
    });
    </script>
<body>
    <input type='text' class='allow_only_float'>
</body>
查看更多
【Aperson】
6楼-- · 2019-02-01 06:08

You can use the parseFloat function.

If the value passed is a float, the function returns the value, otherwise it will return NaN.

Something like:

val = parseFloat(val);
if(isNaN(val))
    alert("not a float!");
查看更多
做自己的国王
7楼-- · 2019-02-01 06:10

To check if a string is an integer or a float

function isFloat(n) {
    return parseFloat(n.match(/^-?\d*(\.\d+)?$/))>0;
}

//alert(isFloat("3.444"));
查看更多
登录 后发表回答