Jump to content

check if variable is number, decimal, positive/negative


2000a

Recommended Posts

Hi,

 

How can I check if a variable only has these characters:

- number (0-9)

- decimal point (.)

- positive (+)

- negative (-)

- dollar ($)

 

I tried is_numeric, but it doesn't like negative values...

 

I need to add the variable value to another variable. I have to accept numbers (including negative and decimals)...

 

ex:

0.1 = TRUE

-.1 = TRUE

12 = TRUE

1000.00 = TRUE

12a = FALSE

 

 

 

thanks

Link to comment
Share on other sites

You could do it this way (I wasn't sure if spaces were allowed so I added them to the array):

<?php
function is_good($str){
$allowed = array('0','1','2','3','4','5','6','7','8','9', '.', '+','-', '$', ' ');
$arr = str_split($str);
foreach($arr as $itm){
	$itm = (string)$itm;
	if(!in_array($itm, $allowed, true))
		return false;
}
return true;
}

$str = '$12 + 12.01 -.01';
if(is_good($str)){
echo 'good';
}else{
echo 'bad';
}
?>

Link to comment
Share on other sites

thanks The Little Guy.

 

Is there a way to do it on one line?

 

I have this working with the exception of negative+positive values (ignore the $):

if(ereg("^[.0-9]+$", $myVar)) { return TRUE; }

 

If I add "\-" it still doesn't like "-"..

 

So if myVar="-.01", I want it to return TRUE.

 

 

thanks

 

Link to comment
Share on other sites

  • 3 months later...

As this is the 3rd result in the google search results, I thought I'd post the answer I found lurking on some other website.

 

To validate a True Decimal Number.

 

//if the number is not a whole number then its a decimal.
function validateTrueDecimal($v) 
{	return(floor($v) != $v);		}


//A Small Unit Test
$test = array (0.5, 0.12, 0.56, 1, 10, "Apples", -13.4, -5.12178934123487123491912471249124, 1e7, "1e7", "1.2");

foreach($test as $value)
{
if (validateTrueDecimal($value)==true) 
{ echo $value . " - Number is a decimal<br>";}
else 
{echo $value . " - <b>Number is not a decimal</b><br>";}
}	

 

Output of Code -

0.5 - Number is a decimal

0.12 - Number is a decimal

0.56 - Number is a decimal

1 - Number is not a decimal

10 - Number is not a decimal

Apples - Number is not a decimal

-13.4 - Number is a decimal

-5.1217893412349 - Number is a decimal

10000000 - Number is not a decimal

1e7 - Number is not a decimal

1.2 - Number is a decimal

Link to comment
Share on other sites

The "sledge-hammer" approach...

<?PHP
$os = "abc $ 123.4 + - wowoow#";

function CheckThisNumber($os) {
/* first remove all invalid characters */
$string = preg_replace("/[^0-9\|\.\-\+\$]/", "", $os);
/* next compare original to new - if not the same then bad */
if($string != $os) {
	return FALSE;
}
/* count the occurrences of +, -, . and $ */
$c_minus = substr_count($os,"-");
$c_plus = substr_count($os,"+");
$c_dec = substr_count($os,".");
$c_dollar =substr_count($os,"$");

/* if there are both - and + then bad */
if($c_minus>0 AND $c_plus>0) {
	return FALSE;
}

/* if there are more than one occurrance of + or - or . or $ then bad */
if($c_minus>1 OR $c_plus>1 OR $c_dec>1 OR $c_dollar>1) {
	return FALSE;
}

/* there is a + or -  AND it is NOT the first character then bad */
if(($c_minus==1 and $os[0] !="-") OR ($c_plus==1 and $os[0] !="+")) {
	return FALSE;
}
/* if there is a - or + AND there is a $ AND the $ is not the 2nd character then bad */
if(($c_minus==1 and $c_dollar==1 and $os[1] !="$") OR ($c_plus==1 and $c_dollar==1 and $os[1] !="$")) {
	return FALSE;
}

/* if there is NO - AND NO + BUT there is a $ AND the $ is not the 1st character then bad */

if($c_minus<1 and $c_plus<1 and $c_dollar ==1 and $os[0] !="$") {
	return FALSE;
}

return TRUE;
}
echo $os . "<br>";
if(!CheckThisNumber($os)) {
echo "BAD";
}else{
echo "GOOD";
}
echo "<br>";
$os = "-$1234.56";
echo $os . "<br>";
if(!CheckThisNumber($os)) {
echo "BAD";
}else{
echo "GOOD";
}
echo "<br>";
$os = "$-1234.56";
echo $os . "<br>";
if(!CheckThisNumber($os)) {
echo "BAD";
}else{
echo "GOOD";
}
echo "<br>";
$os = "-$$1234.56";
echo $os . "<br>";
if(!CheckThisNumber($os)) {
echo "BAD";
}else{
echo "GOOD";
}

?>

Link to comment
Share on other sites

I've just done a wee modifcation of the code berridgeab was working on, it seems to be working a bit better than his, and might be able to help you out as well.

 

<?php
$test = array (+0.5, 0.12, 0.56, 1, 10, "Apples", -13.4, -5.12178934123487123491912471249124, 1e7, "1e7","$654.43","$4,324.43", "1s7", "1.2");
foreach($test as $value):
if(substr($value,0,1) == '$'):
	$value = substr($value,1);
endif;
$value = str_replace(',','',$value);
$oldvalue = $value;
if(!is_numeric($value)):
	settype($value,'float');
endif;
if($value === $oldvalue):
	echo "{$oldvalue} - Number is a decimal<br>";
else:
	echo "{$oldvalue} - Number is NOT a decimal<br>";
endif;
endforeach;
?>

 

And it returns the following.

 

0.5 - Number is a decimal
0.12 - Number is a decimal
0.56 - Number is a decimal
1 - Number is a decimal
10 - Number is a decimal
Apples - Number is NOT a decimal
-13.4 - Number is a decimal
-5.1217893412349 - Number is a decimal
10000000 - Number is a decimal
1e7 - Number is a decimal
654.43 - Number is a decimal
4324.43 - Number is a decimal
1s7 - Number is NOT a decimal
1.2 - Number is a decimal

 

Edit: I refined it a little to accept "$" and commas, the only reason 1e7 seems to be accepted, is 'cause it's the same as "10,000,000".

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.