[ic] [interchange] Add numeric ordercheck
Mike Heins
mike at perusion.com
Thu Mar 25 16:04:07 UTC 2010
Quoting Jon Jensen (interchange-cvs at icdevgroup.org):
> commit 528ff5442ed28dc74a665f81e43d132a072c15b7
> Author: Jon Jensen <jon at endpoint.com>
> Date: Thu Mar 25 09:36:54 2010 -0600
>
> Add numeric ordercheck
>
> Allowed:
> 1
> 0.5
> -1
> -1.0
> -1.0000
> -0.5
>
> Disallowed:
> 1.
> .1
> 1.1.1
> +1
> 1,0
>
> Thanks to Ton Verhagen for making it happen.
Hmm. This is no definition of numeric I know of.
If it is for a SQL purpose, perhaps. I don't know enough to check
that. I will tell you MySQL and Postgres both accept +1, 1. and .1 just
fine, as does Perl.
I think perhaps I would prefer:
use Scalar::Util qw/looks_like_number/;
sub {
my ($ref, $name, $value, $msg) = @_;
use Scalar::Util qw/looks_like_number/;
if (looks_like_number($value)) {
return (1, $name, '');
}
else {
return (0, $name, defined($msg) ? $msg : 'not numeric');
}
}
Here is a test:
my @tests = qw/
True:
1
0.5
-1
-1.0
-1.0000
-0.5
Disallowed:
1.
.1
1.1.1
+1
1,0
/;
use Scalar::Util qw/looks_like_number/;
for(@tests) {
my $result = looks_like_number($_) ? 'true' : 'false';
printf "\ttesting %-20s: %s\n", "'$_'", $result;
}
This prints:
testing 'True:' : false
testing '1' : true
testing '0.5' : true
testing '-1' : true
testing '-1.0' : true
testing '-1.0000' : true
testing '-0.5' : true
testing 'Disallowed:' : false
testing '1.' : true
testing '.1' : true
testing '1.1.1' : false
testing '+1' : true
testing '1,0' : false
--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
phone +1.765.328.4479 <mike at perusion.com>
Just because something is obviously happening doesn't mean something
obvious is happening. --Larry Wall
More information about the interchange-users
mailing list