Hi Monks
I came across with a strange result when I'm using int() function. For example,
my $x1 = (1.15*170)+0.50;
my $x2 = int($x1);
print "Number is: $x1, Integer part is: $x2";
It prints "Number is: 196, Integer part is: 195", but I would expect $x2 to be 196
But when I am using different numbers I am getting the correct results. For example,
my $x1 = (1.15*150)+0.50;
my $x2 = int($x1);
print "\nNumber is: $x1, Integer part is: $x2";
my $x1 = (1.15*30)+0.50;
my $x2 = int($x1);
print "\nNumber is: $x1, Integer part is: $x2";
Can anybody guess what goes wrong with the first one, what goes wrong when I'm using the number 170? At least, I would expect either all results be correct or all results be wrong, but not just one be wrong.
Using int(sprintf("%.2f",$x1)) fixes the error but this is not the issue.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.