SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Round Prices to nearest 10p?

    Hi all,

    I've got prices for products stored in a database which are in the format 12499 (meaning 124.99), and also a discount which is in the format 20 (meaning 20%).

    The current formula I use to format the prices is as follows:

    PHP Code:
    $no1 $price// i.e. 12499
    $no2 $discount// i.e. 20
    $no3 = ($no1/100)*$no2// $no3 now equals $no1 - 20%
    echo number_format((($no1-$no3)/100),2)); // output in the format 100.00 
    This works perfectly but my client has requested that all prices are rounded up to the nearest 10p, so 327.64 would be 327.70 for example.

    How do I go about this?

    Sam
    Sam Hastings

  2. #2
    SitePoint Evangelist sysice's Avatar
    Join Date
    Oct 2004
    Location
    Hackettstown, NJ
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the last line to:

    PHP Code:
    echo number_format(round(($no1-$no3)/100,1),2); 
    You can use floor() or ceil() instead of round if you want to round up or down everytime.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, didn't realise it was that simple.

    Thanks though!

    Sam
    Sam Hastings


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
  •