How can I check if a string is a float?

前端 未结 14 1771
清歌不尽
清歌不尽 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:33

    Use This:

    var isNumber = /^\d+\.\d+$/.test(value);
    
    0 讨论(0)
  • 2020-12-23 20:34

    Only if value is passed in as a string can we fully determine if it uses a decimal point or not. Because 1.0 (as a number) results in 1 though "1.0" (as a string) results in "1.0" exactly the same. And from there we can find if it contains a decimal point, .. So we need to pass the argument value as a string.

    The following will work if value is a string

    if ( value.indexOf('.') > -1 ) { // value is a floating point
    
    }
    

    value.toString() will not turn 1.0 to "1.0" (rather, it would simply turn it to 1) so passing by string is the best alternative because it maintains all of its characters.

    If you do not wish to use a string, then there is no way of capturing 1.0 as a floating-point value. Use the following if you want to test of a number is a floating point value:

    The following will not work for 1.0, 1.00, etc.

    if ( value >>> 0 !== x ) { // value is a floating point
    
    }
    

    Note: You should also check for !isNaN(value) (I left that out to focus on the changes).

    0 讨论(0)
  • 2020-12-23 20:40

    In my case a very simple regex did the trick.

    I needed to validate an user's input whether the input is a valid monetary value. I simply used-

    /^[0-9]+(\.)?[0-9]*$/.test(number)
    

    everything between // is the Regular Expression.

    /^ means the match starts from the beginning of the word and $/ means the match ends with the word. If you think the word can not start with the letter 0 then the expression would be like [1-9][0-9]*

    [0-9]+ means the word must start with at least one number.

    Note: * means zero or more, + means one or more and ? means one or none.

    Up to here the expression is /^[1-9][0-9]*$/ and this would validate only the integer numbers.

    To test for a period (.) in the number we need to use \. with the expression. . is a special character which matches everything, \. will only match the period.

    Finally another character class [0-9]* would match with zero or more digits.

    Test Cases

    /^[0-9]+(\.)?[0-9]$/.test("21.38a") // ==> false
    /^[0-9]+(\.)?[0-9]$/.test("21.38") // ==> true
    /^[0-9]+(\.)?[0-9]$/.test("y2781.68") // ==> false
    /^[0-9]+(\.)?[0-9]$/.test("2781r.68") // ==> false
    
    0 讨论(0)
  • 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+)?$/);
    }
    
    0 讨论(0)
  • 2020-12-23 20:44
    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

    0 讨论(0)
  • 2020-12-23 20:47

    Like this:

    if (!isNaN(value) && value.toString().indexOf('.') != -1)
    {
        alert('this is a numeric value and I\'m sure it is a float.');
    }​
    
    0 讨论(0)
提交回复
热议问题