2010年3月22日 星期一

Perl: Math::Round

Perl installation

  • Perl installation if it wasn't previously installed:
apt-get install perl
  • Perl Math Round library installation:
apt-get install libmath-round-perl
  • AppConfig and DBI perl modules installation:
apt-get install libappconfig-perl libdbi-perl


nearest TARGET, LIST

Rounds the number(s) to the nearest multiple of the target value. TARGET must be positive. In scalar context, returns a single value; in list context, returns a list of values. Numbers that are halfway between two multiples of the target will be rounded to infinity. For example:

  nearest(10, 44)    yields  40
nearest(10, 46)            50
nearest(10, 45)            50
nearest(25, 328)          325
nearest(.1, 4.567)          4.6
nearest(10, -45)          -50
===========================================
#!/usr/local/bin/perl
  use POSIX qw(ceil floor);

  $num = 42.4;  # The Answer to the Great Question (on a Pentium)!

  print "Floor returns: ", floor($num), "\n";
  print "Ceil returns:  ", ceil($num), "\n";

沒有留言: