myfayt
09-04-2011, 08:41 PM
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']
ASTP001
09-04-2011, 08:50 PM
$percent_health = ($user['currenthealth']/$user['maxhealth'])*100;
echo $percent_health."%";
Is that what you were asking for?
myfayt
09-04-2011, 10:15 PM
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?
Spookster
09-04-2011, 10:19 PM
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
Chris Hick
09-04-2011, 10:23 PM
You can use three different things to get that to the way you want it. Using ceil or floor functions or round.
// 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."%";
myfayt
09-04-2011, 10:29 PM
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.
$percent_health = ceil(($user['currenthp']/$user['maxhp']))*100;
Chris Hick
09-04-2011, 10:36 PM
That is because you have your parentheses wrong. You need to copy it like I have it. See if that works.
myfayt
09-04-2011, 10:56 PM
Good catch! I missed that.