Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Unhappy finding the percentage saved from 2 prices using php?

    Hi All,

    i have hundreds of products in my db all with different prices and offer prices and i am trying to work out how to get the percentage the user saves. but i was never any good at maths

    for example

    the original price is £220.99
    the offer price is £200.99
    difference of £20.00

    but i have no idea how to work out the percentage saved?

    can anyone help me with this please
    many thanks

    Luke

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    This isn't a PHP problem. It's a 5th grade math problem. Google and Wikipedia are your friends.
    http://en.wikipedia.org/wiki/Percentage
    Are you a Help Vampire?

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok solved it thanks

    this is the solution for anyone else
    PHP Code:
    $percentage = ( ( high price low price ) / high price ) * 100
    hope this helps someone

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I would then round the results.

    I don't have PHP running at the moment so I'm guessing that 9.050183266 would be 9.05
    PHP Code:
    $percentage round((( high price low price ) / high price ) * 1002); 


    -----------------------
    Leonard Whistler


  •  

    Posting Permissions

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