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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.