Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 24th, 2008, 05:25 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Default Rounding problem

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
Default 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
Default 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
Default 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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles