Connecting Tech Pros Worldwide Help | Site Map

Rounding problem

  #1  
Old March 24th, 2008, 05:25 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Hi,

I'm using PHP 4.4.4. I'm confused about the function "round". I have

<?= "prov tax: $provincial_tax " . round($provincial_tax, 2) . "<BR>
\n" ?>

When $provincial_tax = 7.185, the output above is

prov tax: 7.185 7.18

Shouldn't the second number be 7.19 since 7.185 rounds to 7.19? I
appreciate any insights you may have, - Dave
  #2  
Old March 24th, 2008, 05:35 PM
PaulB
Guest
 
Posts: n/a

re: Rounding problem


laredotornado@zipmail.com wrote:
Quote:
Hi,
>
I'm using PHP 4.4.4. I'm confused about the function "round". I have
>
<?= "prov tax: $provincial_tax " . round($provincial_tax, 2) . "<BR>
\n" ?>
>
When $provincial_tax = 7.185, the output above is
>
prov tax: 7.185 7.18
>
Shouldn't the second number be 7.19 since 7.185 rounds to 7.19? I
appreciate any insights you may have, - Dave
<?php
$provincial_tax = 7.185;
$provtax = round($provincial_tax, 2);
echo $provtax;
?>

I ran that in DzSoft PHP Editor 4.1.2. and it gave me 7.19, then I ran it on
my server running PHP version 5.2.5 and it gave 7.19 so what's the problem?
Maybe it's the PHP version.

Paul
--
Add an underscore after the p to reply


  #3  
Old March 24th, 2008, 05:55 PM
Jerry Stuckle
Guest
 
Posts: n/a

re: Rounding problem


laredotornado@zipmail.com wrote:
Quote:
Hi,
>
I'm using PHP 4.4.4. I'm confused about the function "round". I have
>
<?= "prov tax: $provincial_tax " . round($provincial_tax, 2) . "<BR>
\n" ?>
>
When $provincial_tax = 7.185, the output above is
>
prov tax: 7.185 7.18
>
Shouldn't the second number be 7.19 since 7.185 rounds to 7.19? I
appreciate any insights you may have, - Dave
This has been discussed many times in this forum, and it has to do with
the internal representation of floating point numbers in the hardware.
7.185 is a repeating decimal (like 1/3 is in decimal) and cannot be
represented exactly.

The value is probably not 7.185, but something like 7.184999999345.
Printed, it shows 7.185. But rounding will take it to 7.184, which is
correct. If you want to round to 7.185, you have to add a fudge factor,
i.e.
round($provincial_tax + 0.0001);

Or do everything using integers and only change it when you go to display.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #4  
Old March 25th, 2008, 12:05 PM
The Natural Philosopher
Guest
 
Posts: n/a

re: Rounding problem


laredotornado@zipmail.com wrote:
Quote:
Hi,
>
I'm using PHP 4.4.4. I'm confused about the function "round". I have
>
<?= "prov tax: $provincial_tax " . round($provincial_tax, 2) . "<BR>
\n" ?>
>
When $provincial_tax = 7.185, the output above is
>
prov tax: 7.185 7.18
>
Shouldn't the second number be 7.19 since 7.185 rounds to 7.19? I
appreciate any insights you may have, - Dave
7.185 does not 'round' either way..its a 50-50 choice to round it up or
down.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding Problem Jason answers 5 May 31st, 2006 10:05 AM
python rounding problem. chun ping wang answers 14 May 13th, 2006 04:45 PM
DTS Rounding problem GeorgeŽ answers 1 February 21st, 2006 01:55 PM
Decimal rounding problem Rick Gabel answers 0 November 16th, 2005 08:42 AM