Check if number is decimal

前端 未结 17 1704
生来不讨喜
生来不讨喜 2020-12-09 14:53

I need to check in PHP if user entered a decimal number (US way, with decimal point: X.XXX)

Any reliable way to do this?

相关标签:
17条回答
  • 2020-12-09 15:01

    A total cludge.. but hey it works !

    $numpart = explode(".", $sumnum); 
    
    if ((exists($numpart[1]) && ($numpart[1] > 0 )){
    //    it's a decimal that is greater than zero
    } else {
    // its not a decimal, or the decimal is zero
    }
    
    0 讨论(0)
  • 2020-12-09 15:03

    i use this:

    function is_decimal ($price){
      $value= trim($price); // trim space keys
      $value= is_numeric($value); // validate numeric and numeric string, e.g., 12.00, 1e00, 123; but not -123
      $value= preg_match('/^\d$/', $value); // only allow any digit e.g., 0,1,2,3,4,5,6,7,8,9. This will eliminate the numeric string, e.g., 1e00
      $value= round($value, 2); // to a specified number of decimal places.e.g., 1.12345=> 1.12
    
      return $value;
    }
    
    0 讨论(0)
  • 2020-12-09 15:06

    If you are working with form validation. Then in this case form send string. I used following code to check either form input is a decimal number or not. I hope this will work for you too.

    function is_decimal($input = '') {
    
        $alphabets = str_split($input);
        $find = array('0','1','2','3','4','5','6','7','8','9','.'); // Please note: All intiger numbers are decimal. If you want to check numbers without point "." then you can remove '.' from array. 
    
        foreach ($alphabets as $key => $alphabet) {
            if (!in_array($alphabet, $find)) {
                return false;
            }
        }
    
        // Check if user has enter "." point more then once.
        if (substr_count($input, ".") > 1) {
            return false;
        }
    
        return true;
    }
    
    0 讨论(0)
  • 2020-12-09 15:10

    if you want "10.00" to return true check Night Owl's answer

    If you want to know if the decimals has a value you can use this answer.

    Works with all kind of types (int, float, string)

    if(fmod($val, 1) !== 0.00){
        // your code if its decimals has a value
    } else {
        // your code if the decimals are .00, or is an integer
    }
    

    Examples:

    (fmod(1.00,    1) !== 0.00)    // returns false
    (fmod(2,       1) !== 0.00)    // returns false
    (fmod(3.01,    1) !== 0.00)    // returns true
    (fmod(4.33333, 1) !== 0.00)    // returns true
    (fmod(5.00000, 1) !== 0.00)    // returns false
    (fmod('6.50',  1) !== 0.00)    // returns true
    

    Explanation:

    fmod returns the floating point remainder (modulo) of the division of the arguments, (hence the (!== 0.00))

    Modulus operator - why not use the modulus operator? E.g. ($val % 1 != 0)

    From the PHP docs:

    Operands of modulus are converted to integers (by stripping the decimal part) before processing.

    Which will effectively destroys the op purpose, in other languages like javascript you can use the modulus operator

    0 讨论(0)
  • 2020-12-09 15:11

    the easy way to find either posted value is integer and float so this will help you

    $postedValue = $this->input->post('value');
    if(is_numeric( $postedValue ) && floor( $postedValue ))
    {
        echo 'success';
    }
    else
    {
       echo 'unsuccess';
    }
    

    if you give 10 or 10.5 or 10.0 the result will be success if you define any character or specail character without dot it will give unsuccess

    0 讨论(0)
  • 2020-12-09 15:13

    The function you posted is just not PHP.

    Have a look at is_float [docs].

    Edit: I missed the "user entered value" part. In this case you can actually use a regular expression:

    ^\d+\.\d+$
    
    0 讨论(0)
提交回复
热议问题