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 8 of 8
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Percentage amount

    I am looking at displaying a text percentage amount. So if your life is a maximum of 1,000 and you currently have 200 health, it shows Health: 20%

    Anyone know how I can take 0 to the max health number?

    0 = 0%
    1 = 1%
    Max Health = 100%

    and whatever in between. This would be a huge help, thank you.

    Edit: By the way it'd be grabbing database values like $user['currenthealth'] and $user['maxhealth']
    Last edited by myfayt; 09-04-2011 at 10:56 PM.

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    PHP Code:
    $percent_health = ($user['currenthealth']/$user['maxhealth'])*100;
    echo 
    $percent_health."%"
    Is that what you were asking for?

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Thanks for the reply. It didn't quite work, the result came to:

    Current HP: 74.3726235741%
    I am guessing that means 74%? Does it just need to be floored or is there a way to make sure it's always a solid number with no decimals?

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by myfayt View Post
    Thanks for the reply. It didn't quite work, the result came to:



    I am guessing that means 74%? Does it just need to be floored or is there a way to make sure it's always a solid number with no decimals?
    Then round the value.
    http://php.net/manual/en/function.round.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    You can use three different things to get that to the way you want it. Using ceil or floor functions or round.

    PHP Code:
    // ceil function
    // this rounds everything up
    $percent_health ceil(($user['currenthp']/$user['maxhp'])*100);
    echo 
    $percent_health."%";
     
    //floor function 
    // this rounds everything down
    $percent_health floor(($user['currenthp']/$user['maxhp'])*100);
    echo 
    $percent_health."%";  
     
    // round
    // this rounds anything <5 down and anything >= 5 up
    $percent_health round(($user['currenthp']/$user['maxhp'])*100);
    echo 
    $percent_health."%"
    Last edited by Chris Hick; 09-04-2011 at 10:40 PM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #6
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    My health should only be in the 70's but it's saying 100% so it isn't right. I tried round and ceil both.

    PHP Code:
     $percent_health ceil(($user['currenthp']/$user['maxhp']))*100

  • #7
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    That is because you have your parentheses wrong. You need to copy it like I have it. See if that works.
    Last edited by Chris Hick; 09-04-2011 at 10:41 PM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #8
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Good catch! I missed that.


  •  

    Posting Permissions

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