# Thread: Problem using Round - any ideas

1. ## Problem using Round - any ideas

Hi

I'm having trouble with using round to round a vat calculation. I have done separated out the lines that I believe show the problem.

I can't work out why the first sprintf produces an incorrect rounded value, yet the second sprintf produces the required results. Driving me mad - can anyone put my mind at rest and stop my hair from turning grey? I'm sure it's something simple.

The code is:

<?php
\$number = 691.30;
\$withvat = \$number * 1.15;
\$total = \$number * 1.15;
\$vat = \$total - \$number;
?>
<?=sprintf("%.2F", round(\$vat, 2));?><br>
<?=sprintf("%.2F", round(103.695, 2));?>

2. Originally Posted by mambo-no-6
Hi

I'm having trouble with using round to round a vat calculation. I have done separated out the lines that I believe show the problem.

I can't work out why the first sprintf produces an incorrect rounded value, yet the second sprintf produces the required results. Driving me mad - can anyone put my mind at rest and stop my hair from turning grey? I'm sure it's something simple.

The code is:

<?php
\$number = 691.30;
\$withvat = \$number * 1.15;
\$total = \$number * 1.15;
\$vat = \$total - \$number;
?>
<?=sprintf("%.2F", round(\$vat, 2));?><br>
<?=sprintf("%.2F", round(103.695, 2));?>

http://en.php.net/round

check out the manual. More people have encountered this problem. As a result some of them wrote their own function. Maybe you could look into that.

3. It's always better to do math with money using integer cents/pence values rather than float dollars/pounds

But why round to 2dp when you're using sprintf to format to 2dp anyway

4. By the way, you would be better off using:
PHP Code:
``` <?php\$number = 691.3;\$vat = \$number * 0.15;\$total = \$number + \$vat;?>....<?php printf('£%.2f', \$vat); ?> ```

5. Keep in mind, printf() truncates
PHP Code:
``` printf('%.2f', 1.555); // 1.55 echo round(1.555, 2); // 1.6  ```