Re: floating point validation
by moritz (Cardinal) on Aug 30, 2010 at 12:04 UTC
|
From skimming the CB it seems that the original problem (whatever it was) was solved, and this is the solution.
The question seemed to be how to validate a given string against a number format with given number of decimals before and after the decimal point.
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] |
Re: floating point validation
by ikegami (Patriarch) on Aug 30, 2010 at 14:03 UTC
|
| [reply] [d/l] |
Re: floating point validation
by govindkailas (Acolyte) on Aug 30, 2010 at 12:06 UTC
|
here is the corrected one :)
thanks to all ..
$val=101.24;
validate(3,2,$val);
sub validate{
$v1=$_[0];
$v2=$_[1];
$v3=$_[2];
print "Values are $v1 \t $v2 \t $v3 \n";
if($v3=~m/^\d{$v1}\.\d{$v2}$/ ){
print "its a good one\n";}
else {print "Not Good!!";}
}
| [reply] [d/l] |
|
I tidied the script for you:).
#!/usr/bin/perl
use strict;
use warnings;
my $val = 101.24;
validate( 3, 2, $val );
sub validate {
my $v1 = $_[0];
my $v2 = $_[1];
my $v3 = $_[2];
print "Values are $v1 \t $v2 \t $v3 \n";
if ( $v3 =~ m/^\d{$v1}\.\d{$v2}$/ ) {
print "its a good one\n";
}
else {
print "Not Good!!";
}
}
| [reply] [d/l] |
|
my $val = 101.24;
print match_decimal_format( 3, 2, $val ) ? "Valid!\n" : "Invalid...\n"
+;
sub match_decimal_format {
my ( $leading, $trailing, $number ) = @_;
return $number =~ m/^ [0-9]{$leading} \. [0-9]{$trailing} $/x;
}
| [reply] [d/l] |
Re: floating point validation
by ww (Archbishop) on Aug 30, 2010 at 12:46 UTC
|
When posting, please include an explicit question. Your OP can be interpreted in many ways; most of them utterly unrelated to your actual intent. | [reply] |
Re: floating point validation
by trwww (Priest) on Aug 30, 2010 at 17:57 UTC
|
use Regexp::Common;
if ( $RE{num}{real}->matches($v3) ) {
print "its a good one\n";
} else {
print "Not Good!!";
}
Regexp::Common is one of the best libraries on CPAN!
| [reply] [d/l] |
Re: floating point validation
by sundialsvc4 (Abbot) on Aug 30, 2010 at 21:53 UTC
|
It is also worth mentioning that it’s frequently simplest to just attempt to convert the string input to a number ... and to catch any exception that might be thrown. Instead of testing the string each time to see if it “looks like a number,” you simply assume that it does, and catch the (rare) case where it turns out that it doesn’t.
| |
Re: floating point validation
by aquarium (Curate) on Aug 31, 2010 at 00:40 UTC
|
| [reply] |