0

I am trying to round the double 0.0045. When I call...

echo round($theFloat, 3); 

I get 0.004, instead of 0.005 which is the expected response.

Here is the code:

$increase = 1.1; $previousPrice = round(0.11 / $increase, 2); $nextPrice = round(0.11 * $increase, 2); $afterCut = round(0.11 * 0.95, 6); $willSend = $afterCut - $previousPrice; echo round($willSend, 3); 
10
  • var_dump($willSend); and see what you got. Commented Jan 29, 2013 at 4:36
  • Can you echo $willSend to verify what is getting passed to round()? Commented Jan 29, 2013 at 4:36
  • Check all your inputs, code looks fine. Commented Jan 29, 2013 at 4:37
  • I know it looks fine, but I can't figure out what's wrong with it and why it rounds it to 0.004! Commented Jan 29, 2013 at 4:38
  • echo round(0.0045,3); this is giving output as 5. Commented Jan 29, 2013 at 4:39

2 Answers 2

1

Although what you have looks like 0.0045, the value is actually approximately 0.00449999999999999966, since IEEE 754 floating point values cannot specify exactly that number. And that value, when rounded to 3 places, is 0.004.

Sign up to request clarification or add additional context in comments.

Comments

1

Try like this

$increase = 1.1; $previousPrice = 0.11 / $increase; $nextPrice = 0.11 * $increase; $afterCut = round(0.11 * 0.95, 6); $willSend = $afterCut - $previousPrice; echo round($willSend, 3); 

2 Comments

But i dont knw what is the value of $r[1] which you have used
Already gladoscc says $r[1] is 0.11 so there is no need to put static in place of $r[1]....so sujit singh no need to edit my ans right..??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.