SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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));?>
    Mambo-No-6
    ...always aiming to go one better...

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mambo-no-6 View Post
    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.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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); ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind, printf() truncates
    PHP Code:
    printf('%.2f'1.555); // 1.55
    echo round(1.5552); // 1.6 


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •