Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Wierd Numbers!

Status
Not open for further replies.

Visiting

IS-IT--Management
Nov 26, 2002
21
0
0
US
I want ot print a simple list of monetary value from $5 - $15 in increments of nickels, but using the code:

for ($j = 5; $j <= 15; $j += .05){
print &quot;\$$j<p>&quot;;
}

prints out:
$5 $5.05 $5.1 $5.15 $5.2 $5.25 $5.3 $5.35 $5.4 $5.45 $5.5 $5.55 $5.6 $5.65 $5.7 $5.75 $5.8 $5.85 $5.9 $5.95 $6 $6.05 $6.1 $6.15 $6.2 $6.25 $6.3 $6.35 $6.4 $6.44999999999999 $6.49999999999999 $6.54999999999999 $6.59999999999999 $6.64999999999999 $6.69999999999999 $6.74999999999999 $6.79999999999999 $6.84999999999999 $6.89999999999999 $6.94999999999999 $6.99999999999999 $7.04999999999999 $7.09999999999999 $7.14999999999999 $7.19999999999999 $7.24999999999999 $7.29999999999999 $7.34999999999999 $7.39999999999999 $7.44999999999999 $7.49999999999999 $7.54999999999999 $7.59999999999999 $7.64999999999999 $7.69999999999999 $7.74999999999999 $7.79999999999999 $7.84999999999999 $7.89999999999999 $7.94999999999999 $7.99999999999999 $8.04999999999999 $8.09999999999999 $8.14999999999999 $8.19999999999999 $8.24999999999999 $8.29999999999999 $8.34999999999999 $8.4 $8.45 $8.5 $8.55 $8.6 $8.65 $8.7 $8.75 $8.8 $8.85 $8.9 $8.95 $9 $9.05 $9.1 $9.15000000000001 $9.20000000000001 $9.25000000000001 $9.30000000000001 $9.35000000000001 $9.40000000000001 $9.45000000000001 $9.50000000000001 $9.55000000000001 $9.60000000000001 $9.65000000000001 $9.70000000000001 $9.75000000000001 $9.80000000000001 $9.85000000000002 $9.90000000000002 $9.95000000000002 $10 $10.05 $10.1 $10.15 $10.2 $10.25 $10.3 $10.35 $10.4 $10.45 $10.5 $10.55 $10.6 $10.65 $10.7 $10.75 $10.8 $10.85 $10.9 $10.95 $11 $11.05 $11.1 $11.15 $11.2 $11.25 $11.3 $11.35 $11.4 $11.45 $11.5 $11.55 $11.6 $11.65 $11.7 $11.75 $11.8 $11.85 $11.9 $11.95 $12 $12.05 $12.1 $12.15 $12.2 $12.25 $12.3000000000001 $12.3500000000001 $12.4000000000001 $12.4500000000001 $12.5000000000001 $12.5500000000001 $12.6000000000001 $12.6500000000001 $12.7000000000001 $12.7500000000001 $12.8000000000001 $12.8500000000001 $12.9000000000001 $12.9500000000001 $13.0000000000001 $13.0500000000001 $13.1000000000001 $13.1500000000001 $13.2000000000001 $13.2500000000001 $13.3000000000001 $13.3500000000001 $13.4000000000001 $13.4500000000001 $13.5000000000001 $13.5500000000001 $13.6000000000001 $13.6500000000001 $13.7000000000001 $13.7500000000001 $13.8000000000001 $13.8500000000001 $13.9000000000001 $13.9500000000001 $14.0000000000001 $14.0500000000001 $14.1000000000001 $14.1500000000001 $14.2000000000001 $14.2500000000001 $14.3000000000001 $14.3500000000001 $14.4000000000001 $14.4500000000001 $14.5000000000001 $14.5500000000001 $14.6000000000001 $14.6500000000001 $14.7000000000001 $14.7500000000001 $14.8000000000001 $14.8500000000001 $14.9000000000001 $14.9500000000001
 
Wow! Thanks! But, why is it still only going to $14.95 when I have $i <= 15?
 
That's because perl cannot accurately represent floating point numbers. If you stick a &quot;print $j&quot; after the loop, you will see it is 15.0000000001

You should round J as it goes thru the loop:

for ($j = 5; round($j,2) <= 15; $j+=0.05){
printf(&quot;\$%.2f %f\n&quot;,$j,$j);

}

Add this sub to round (to avoid having to use Lib::Math

sub round {
my ($number,$decimals) = @_;
return substr($number+(&quot;0.&quot;.&quot;0&quot;x$decimals.&quot;5&quot;),
0, $decimals+length(int($number))+1);
}


=================================
Thomas V. Flaherty Jr.
Birch Hill Technology Group, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top