in reply to Re: How to determine whether a number has a decimal component?
in thread How to determine whether a number has a decimal component?
Well you could use
print "contains fractional component\n" if $number =~ /\.(?!0*$)/;
But that'll fail for large numbers.
sub r { $_[0] ? 'fractional' : 'integer' } for my $n ( '1.00', 2**50, ) { print( "$n:\n", "method 1: ", r( $n =~ /\./ ), "\n", "method 2: ", r( $n =~ /\.(?!0*$)/ ), "\n", "method 3: ", r( sprintf('%f',$n) =~ /\.(?!0*$)/ ), "\n", "method 4: ", r( int($_)!=$_ ), "\n", "\n", ); }
1.00: method 1: fractional method 2: integer method 3: integer method 4: integer 1.12589990684262e+015: method 1: fractional method 2: fractional method 3: integer method 4: integer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to determine whether a number has a decimal component?
by Xenofur (Monk) on Jan 04, 2009 at 22:10 UTC | |
by ikegami (Patriarch) on Jan 04, 2009 at 22:20 UTC |