perlfaq nodetype
faq_monk
<P>
Remember that
<CODE>int()</CODE> merely truncates toward 0. For rounding to a certain number of digits,
<CODE>sprintf()</CODE> or
<CODE>printf()</CODE> is usually the easiest route.
<P>
<PRE> printf("%.3f", 3.1415926535); # prints 3.142
</PRE>
<P>
The
<FONT SIZE=-1>POSIX</FONT> module (part of the standard perl distribution) implements
<CODE>ceil(),</CODE>
<CODE>floor(),</CODE> and a number of other mathematical and trigonometric functions.
<P>
<PRE> use POSIX;
$ceil = ceil(3.5); # 4
$floor = floor(3.5); # 3
</PRE>
<P>
In 5.000 to 5.003 Perls, trigonometry was done in the Math::Complex module.
With 5.004, the Math::Trig module (part of the standard perl distribution)
implements the trigonometric functions. Internally it uses the
Math::Complex module and some functions can break out from the real axis
into the complex plane, for example the inverse sine of 2.
<P>
Rounding in financial applications can have serious implications, and the
rounding method used should be specified precisely. In these cases, it
probably pays not to trust whichever system rounding is being used by Perl,
but to instead implement the rounding function you need yourself.
<P>