SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    number_format without rounding

    Hi All

    This is doing my head in!!!!

    I am using:

    if ($country == 'AU') { $hasgst = ''; $gstamt = $unitprice * .1; $exgst = $unitprice - $gstamt; } else { $hasgst = 'n'; $exgst = $unitprice; $gstamt = ""; }

    $unitprice = (number_format($unitprice, 2, '.', ','));
    $exgst = (number_format($exgst, 2, '.', ','));
    $gstamt = (number_format($gstamt, 2, '.', ','));

    Basically this code works out the GST (tax) of the total amount.

    The issue I have is that the current price is $49.95 (this can change to whatever though) and when the code above does its job the number_format option is rounding up putting everything out by .01.

    I have read a pile of post I Googled but they are all different.

    Can anyone please tell me how to stop the rounding?

    Thanks

    mrmbarnes

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Have a look in the PHP manual at the money_format() function, it should suit your needs better then number_format
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    So 49.95;
    10% = 4.995
    total = 54.945

    Your post suggests you want to show 54.94 rather than 54.95. (Why? I'm fairly sure the tax system would round this up. But anyway...)

    money_format still rounds, but is a good currency display.

    floor($total * 100) / 100 will give you the floored value 54.94.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys... the rounding was rounding both up... the 1 cent on tax or one cent of non tax doesn't really matter as that could be open for discussion but the issue I was having is that the total was adding up to $49.96 and not $49.95.

    In any case the money_format() function sorted it all out.

    Thanks once again


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
  •