How check value is decimal or not in php?

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?

Uwe Keim

38.6k56 gold badges173 silver badges280 bronze badges

asked Jul 21, 2011 at 7:22

How check value is decimal or not in php?

CodeVirtuosoCodeVirtuoso

6,08012 gold badges44 silver badges62 bronze badges

6

You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):

function is_decimal( $val )
{
    return is_numeric( $val ) && floor( $val ) != $val;
}

answered Jul 21, 2011 at 7:28

cwallenpoolecwallenpoole

77.3k26 gold badges125 silver badges163 bronze badges

7

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

answered Nov 6, 2014 at 13:23

1

If all you need to know is whether a decimal point exists in a variable then this will get the job done...

function containsDecimal( $value ) {
    if ( strpos( $value, "." ) !== false ) {
        return true;
    }
    return false;
}

This isn't a very elegant solution but it works with strings and floats.

Make sure to use !== and not != in the strpos test or you will get incorrect results.

answered Jan 24, 2012 at 4:07

Night OwlNight Owl

4,1284 gold badges27 silver badges37 bronze badges

1

another way to solve this: preg_match('/^\d+\.\d+$/',$number); :)

answered Jul 21, 2011 at 7:33

How check value is decimal or not in php?

k102k102

7,5137 gold badges48 silver badges69 bronze badges

0

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+$

answered Jul 21, 2011 at 7:26

How check value is decimal or not in php?

Felix KlingFelix Kling

767k171 gold badges1068 silver badges1114 bronze badges

2

I was passed a string, and wanted to know if it was a decimal or not. I ended up with this:

function isDecimal($value) 
{
     return ((float) $value !== floor($value));
}

I ran a bunch of test including decimals and non-decimals on both sides of zero, and it seemed to work.

gen_Eric

217k40 gold badges295 silver badges334 bronze badges

answered Oct 13, 2011 at 16:58

Jeff VJeff V

511 silver badge1 bronze badge

1

is_numeric returns true for decimals and integers. So if your user lazily enters 1 instead of 1.00 it will still return true:

echo is_numeric(1); // true
echo is_numeric(1.00); // true

You may wish to convert the integer to a decimal with PHP, or let your database do it for you.

answered Nov 20, 2013 at 14:15

rybo111rybo111

12k4 gold badges58 silver badges67 bronze badges

1

This is a more tolerate way to handle this with user input. This regex will match both "100" or "100.1" but doesn't allow for negative numbers.

/^(\d+)(\.\d+)?$/

answered Sep 10, 2013 at 18:05

ChaoixChaoix

1,24810 silver badges13 bronze badges

   // if numeric 

if (is_numeric($field)) {
        $whole = floor($field);
        $fraction = $field - $whole;

        // if decimal            
        if ($fraction > 0)
            // do sth
        else
        // if integer
            // do sth 
}
else

   // if non-numeric
   // do sth

answered Jul 31, 2014 at 12:46

Van BienVan Bien

111 silver badge2 bronze badges

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

answered Jul 5, 2014 at 4:19

0

$lat = '-25.3654';

if(preg_match('/./',$lat)) {
    echo "\nYes its a decimal value\n";
}
else{
    echo 'No its not a decimal value';
}

answered Mar 3, 2014 at 11:20

How check value is decimal or not in php?

NeocortexNeocortex

6439 silver badges32 bronze badges

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
}

answered Mar 6, 2015 at 22:22

How check value is decimal or not in php?

Duane LortieDuane Lortie

1,2921 gold badge12 silver badges16 bronze badges

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

answered Dec 25, 2019 at 11:57

How check value is decimal or not in php?

abubakkar tahirabubakkar tahir

6871 gold badge11 silver badges12 bronze badges

How about (int)$value != $value? If true it's decimal, if false it's not.

answered Nov 29, 2021 at 15:49

I can't comment, but I have this interesting behaviour. (tested on v. 7.3.19 on a website for php testing online)

If you multiply 50 by 1.1 fmod gives different results than expected. If you do by 1.2 or 1.3 it's fine, if you do another number (like 60 or 40) is also fine.

$price = 50;
$price = $price * 1.1; 

if(strpos($price,".") !== false){
    echo "decimal";
}else{
    echo "not a decimal";
}

echo '
'; if(fmod($price, 1) !== 0.00){ //echo fmod($price, 1); echo "decimal"; } else { echo "not a decimal"; }//end if

answered Feb 6 at 18:37

1

Simplest solution is

if(is_float(2.3)){

 echo 'true';

}

answered Aug 24, 2017 at 10:06

How check value is decimal or not in php?

Ayyaz ZafarAyyaz Zafar

1,8185 gold badges25 silver badges39 bronze badges

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

answered Dec 15, 2017 at 2:17

How check value is decimal or not in php?

Adnan AhmadAdnan Ahmad

7901 gold badge10 silver badges12 bronze badges

function is_decimal_value( $a ) {
    $d=0; $i=0;
    $b= str_split(trim($a.""));
    foreach ( $b as $c ) {
        if ( $i==0 && strpos($c,"-") ) continue;
        $i++;
        if ( is_numeric($c) ) continue;
        if ( stripos($c,".") === 0 ) {
            $d++;
            if ( $d > 1 ) return FALSE;
            else continue;
        } else
        return FALSE;
    }
    return TRUE;
}

Known Issues with the above function:

1) Does not support "scientific notation" (1.23E-123), fiscal (leading $ or other) or "Trailing f" (C++ style floats) or "trailing currency" (USD, GBP etc)

2) False positive on string filenames that match a decimal: Please note that for example "10.0" as a filename cannot be distinguished from the decimal, so if you are attempting to detect a type from a string alone, and a filename matches a decimal name and has no path included, it will be impossible to discern.

answered Feb 10, 2020 at 19:24

How check value is decimal or not in php?

Maybe try looking into this as well

!is_int()

answered Dec 23, 2012 at 4:13

How check value is decimal or not in php?

Cam TullosCam Tullos

2,4771 gold badge20 silver badges17 bronze badges

0

Not the answer you're looking for? Browse other questions tagged php function decimal or ask your own question.

How can I check if a number is decimal in PHP?

The PHP is_numeric() function can be used to find whether a variable is numeric. The function returns true if the variable is a number or a numeric string, false otherwise.

How check value is number or not in PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How do you check if a string has decimals?

result = sqrt(stringContainingANumber); decimal = new RegExp("."); document. write(decimal. test(result));