[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] equivalent of "printf" in a subroutine



Hi

I'd like to khow if there an easy way to do the equivalent of :

printf ("%.2f", '23.4567');

but in a function, like this :

sub round {
 ($number) = @_;
 $nbr = $number * (1000);
 $lastDigit = (int $nbr) % 10;
 $nbr /= 10;
 $nbr = int $nbr;
 if ($lastDigit >= 5) {
  $nbr++;
  }
 return ($nbr / 100);
};

print &round('23.456');    # prints 23.46  OK
print &round('23.4547');  # prints 23.45  OK
print &round('23.4');        # prints 23.4    WRONG     I'd like : 23.40

Thanks
Jean-François


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org