Jump to content

check for currency


dadamssg87

Recommended Posts

I'm trying to develop a function to check to see if the value inputted was in a currency format. I'm checking for:

if the value is numeric

if the value is an integer

if not integer check the number of digits to the right of the decimal point

 

I can't tell how the php function is_int() works though. I want to accept integers and also numbers with two decimal places. So accepted values will be "123" and also "123.99".

 

The script tells me the value is not an integer no matter what.

 

<?php
$num = $_POST['number'];
$positionOfDecimalPoint = strpos($num, '.');
$numbersToLeft = substr($num,0,$positionOfDecimalPoint);
$numbersToRight = substr($num,$positionOfDecimalPoint + 1);//Add one so decimal point isn't included in output.

echo "You submitted ".$num."<br><br>";

if(!is_numeric($num))
{
echo "Not Numeric";
}
else
{ 
   if(!is_int($num))
     {	     	
		 $adecimal = strlen($numbersToRight);
		 if($adecimal !== 2)
			{
			  echo "There needs to be exactly two numbers after the decimal.";
			}
			echo "<br><br>The number is not an integer.";
         }else
         {
       echo "The integer is ".$num;
         }
}



?>

Link to comment
https://forums.phpfreaks.com/topic/235650-check-for-currency/
Share on other sites

got it with this...accepts integers and only numbers with two decimal points.

 

<?php
function check_currency($num)
{

if(!is_numeric($num))
  {
	echo "Not Numeric";
  }
else
  {     	
	$decimal_exists = strpos($num, '.');
	if($decimal_exists == TRUE)
      {
	     $after_decimal = strlen(substr($num, $decimal_exists + 1));
		 if($after_decimal !== 2)
		   {
		     echo "There needs to be exactly two digits after the decimal.";
		   }
		   else
		   {
			 echo "You entered the correct format for a decimal.";
		   }
            
	   }
	  else
	   {
		 echo "You entered an acceptable integer.";
	   }
   }
}

Link to comment
https://forums.phpfreaks.com/topic/235650-check-for-currency/#findComment-1211206
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.