ANJAN PURKAYASTHA answered:
ANJAN PURKAYASTHA asked:
I would like to divide a floating point number into its whole and fractional
parts.
So,
($w, $f)= some_subroutine(12.735)
assigns 12 to $w and 0.735 to $f.
Any easy perlish way of doing this?
after some research i was able to answer my own question.
solution:
use POSIX;
$n= 12.735;
$w= floor($n); # assigns the value 12 to $n.
$f= $n-$w; # this holds the fractional value.
print $f outputs 0.734999999999999 on my box.
Owen's solution does not have that problem.
Here is another variant:
sub some_subroutine {
my ($i, $d) = shift =~ /^(\d+)(\.\d+)?$/
or die 'Not a floating point number';
return $i, defined $d ? '0'.$d : 0;
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/