<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PERL Round Function</title>
	<atom:link href="http://www.solarum.com/2008/03/16/perl-round-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solarum.com/2008/03/16/perl-round-function/</link>
	<description>Information for everyone</description>
	<lastBuildDate>Mon, 23 Jan 2012 05:41:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: rodrigo</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-3677</link>
		<dc:creator>rodrigo</dc:creator>
		<pubDate>Fri, 22 May 2009 20:56:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-3677</guid>
		<description>can i round for the next up number?
Eg.:
Number : 16.234
return 17</description>
		<content:encoded><![CDATA[<p>can i round for the next up number?<br />
Eg.:<br />
Number : 16.234<br />
return 17</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim D.</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-1609</link>
		<dc:creator>Tim D.</dc:creator>
		<pubDate>Fri, 20 Mar 2009 01:40:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-1609</guid>
		<description>No, this line is wrong:
$number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);

That introduces an error of rounding up when it should be down.
The only way I can com up with for my test case is to replace that line with:
$number = abs($number)*$dec +.5;
$number =~ s/\..*$//;
which instead of using int to truncate which did not work in my test case, treats it as a sting and truncates the decimal that way.

So, the sub now is:
sub myRound {
  my $number = shift &#124;&#124; 0;
  my $places = shift &#124;&#124; 0;
  my $dec = 10**$places;
  my $sign = ($number &gt;= 0) ? 1 : -1;
  
  #$number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);
  $number = abs($number)*$dec +.5;
  $number =~ s/\..*$//;
  
  # return the number with sign and correct decimal places
  return $number * $sign / $dec;
}</description>
		<content:encoded><![CDATA[<p>No, this line is wrong:<br />
$number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);</p>
<p>That introduces an error of rounding up when it should be down.<br />
The only way I can com up with for my test case is to replace that line with:<br />
$number = abs($number)*$dec +.5;<br />
$number =~ s/\..*$//;<br />
which instead of using int to truncate which did not work in my test case, treats it as a sting and truncates the decimal that way.</p>
<p>So, the sub now is:<br />
sub myRound {<br />
  my $number = shift || 0;<br />
  my $places = shift || 0;<br />
  my $dec = 10**$places;<br />
  my $sign = ($number &gt;= 0) ? 1 : -1;</p>
<p>  #$number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);<br />
  $number = abs($number)*$dec +.5;<br />
  $number =~ s/\..*$//;</p>
<p>  # return the number with sign and correct decimal places<br />
  return $number * $sign / $dec;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim D.</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-1607</link>
		<dc:creator>Tim D.</dc:creator>
		<pubDate>Thu, 19 Mar 2009 22:26:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-1607</guid>
		<description>Correction for last 2 lines:
my $calc = myRound(1900*-18.12895,2)
returns -34445.01</description>
		<content:encoded><![CDATA[<p>Correction for last 2 lines:<br />
my $calc = myRound(1900*-18.12895,2)<br />
returns -34445.01</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim D.</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-1606</link>
		<dc:creator>Tim D.</dc:creator>
		<pubDate>Thu, 19 Mar 2009 22:23:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-1606</guid>
		<description>I ran into an example where that round subroutine doesn&#039;t work:

my $calc = round(1900*18.12895,2)
This returned 34445
The correct value should be 34445.01

This might be due to the fact that int sometimes doesn&#039;t behave as expected (see http://perldoc.perl.org/functions/int.html)
Also, I got strange results from ($number  0) which is supposed to handle negative numbers.

Here is the modification I came up with:

sub myRound {
  my $number = shift &#124;&#124; 0;
  my $places = shift &#124;&#124; 0;
  my $dec = 10**$places;
  my $sign = ($number &gt;= 0) ? 1 : -1;
  
  $number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);
  
  # return the number with sign and correct decimal places
  return $number * $sign / $dec;
}

This gave me the correct result, and it also handles negative numbers:
my $calc - myRound(1900*-18.12895,2)
returns -34445.01</description>
		<content:encoded><![CDATA[<p>I ran into an example where that round subroutine doesn&#8217;t work:</p>
<p>my $calc = round(1900*18.12895,2)<br />
This returned 34445<br />
The correct value should be 34445.01</p>
<p>This might be due to the fact that int sometimes doesn&#8217;t behave as expected (see <a href="http://perldoc.perl.org/functions/int.html" rel="nofollow">http://perldoc.perl.org/functions/int.html</a>)<br />
Also, I got strange results from ($number  0) which is supposed to handle negative numbers.</p>
<p>Here is the modification I came up with:</p>
<p>sub myRound {<br />
  my $number = shift || 0;<br />
  my $places = shift || 0;<br />
  my $dec = 10**$places;<br />
  my $sign = ($number &gt;= 0) ? 1 : -1;</p>
<p>  $number = sprintf(&#8221;%.0f&#8221;,(abs($number)*$dec) +.5);</p>
<p>  # return the number with sign and correct decimal places<br />
  return $number * $sign / $dec;<br />
}</p>
<p>This gave me the correct result, and it also handles negative numbers:<br />
my $calc &#8211; myRound(1900*-18.12895,2)<br />
returns -34445.01</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Update to the PERL round function &#124; Solarum - Information For Everyone</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-436</link>
		<dc:creator>Update to the PERL round function &#124; Solarum - Information For Everyone</dc:creator>
		<pubDate>Fri, 07 Nov 2008 01:02:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-436</guid>
		<description>[...] his addition, you can now specify how many digits to show on the right of the decimal. Pretty neat, check it out.   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] his addition, you can now specify how many digits to show on the right of the decimal. Pretty neat, check it out.   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Laz</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-435</link>
		<dc:creator>Laz</dc:creator>
		<pubDate>Fri, 07 Nov 2008 00:44:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-435</guid>
		<description>Great job, and thanks!  I had to figure out the parts removed by WordPress, but once I got that it works like a champ.  I will update the post and add your modification.

Thanks again!</description>
		<content:encoded><![CDATA[<p>Great job, and thanks!  I had to figure out the parts removed by WordPress, but once I got that it works like a champ.  I will update the post and add your modification.</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thierry H.</title>
		<link>http://www.solarum.com/2008/03/16/perl-round-function/comment-page-1/#comment-434</link>
		<dc:creator>Thierry H.</dc:creator>
		<pubDate>Fri, 07 Nov 2008 00:03:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.solarum.com/2008/03/16/perl-round-function/#comment-434</guid>
		<description>Here is a small enhancement to let a second parameter specifies the # of decimals:

print round(3.14159265,3);  # ==&gt; 3.142

sub round {
   my $nombre = shift &#124;&#124; 0;
   my $dec = 10 ** (shift &#124;&#124; 0);
   #return sprintf(&quot;%.${dec}f&quot;, $nombre);
   return int( $dec * $nombre + .5 * ($nombre  0)) / $dec;
}</description>
		<content:encoded><![CDATA[<p>Here is a small enhancement to let a second parameter specifies the # of decimals:</p>
<p>print round(3.14159265,3);  # ==&gt; 3.142</p>
<p>sub round {<br />
   my $nombre = shift || 0;<br />
   my $dec = 10 ** (shift || 0);<br />
   #return sprintf(&#8220;%.${dec}f&#8221;, $nombre);<br />
   return int( $dec * $nombre + .5 * ($nombre  0)) / $dec;<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

