<?xml version="1.0" encoding="windows-1252"?>
<node id="579" title="Does perl have a round function? What about ceil() and floor()? Trig functions?" created="1999-10-07 20:20:52" updated="2005-08-10 07:44:55">
<type id="834">
perlfaq nodetype</type>
<author id="519">
faq_monk</author>
<data>
<field name="doctext">

&lt;P&gt;
Remember that 
&lt;CODE&gt;int()&lt;/CODE&gt; merely truncates toward 0. For rounding to a certain number of digits, 
&lt;CODE&gt;sprintf()&lt;/CODE&gt; or 
&lt;CODE&gt;printf()&lt;/CODE&gt; is usually the easiest route.

&lt;P&gt;
&lt;PRE&gt;    printf(&amp;quot;%.3f&amp;quot;, 3.1415926535);       # prints 3.142
&lt;/PRE&gt;
&lt;P&gt;
The 
&lt;FONT SIZE=-1&gt;POSIX&lt;/FONT&gt; module (part of the standard perl distribution) implements 
&lt;CODE&gt;ceil(),&lt;/CODE&gt; 
&lt;CODE&gt;floor(),&lt;/CODE&gt; and a number of other mathematical and trigonometric functions.

&lt;P&gt;
&lt;PRE&gt;    use POSIX;
    $ceil   = ceil(3.5);                        # 4
    $floor  = floor(3.5);                       # 3
&lt;/PRE&gt;
&lt;P&gt;
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.

&lt;P&gt;
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.

&lt;P&gt;
</field>
</data>
</node>
