php percentage help

Associate
Joined
11 Oct 2008
Posts
268
I have made a 'health bar' for a simple browser game i have been working on.

I want to use a background image to fill the health bar, so if for example, the player had full health, the health bar image width would be 100%

is it possible to say set, the characters max health and the characters actual health and work it out as a percentage so i can stretch the image width as far as it needs to go?


sorry if i haven't been too clear, my understanding of maths is not so good.

Thanks for any tips.
 
Associate
Joined
19 Jul 2006
Posts
1,847
Don't really understand. But easiest thing to do is set max health to 100 units and then everything is in % ?
 
Associate
OP
Joined
11 Oct 2008
Posts
268
edit: so i this working now

$current_health=250;
$max_health=9999;
$health_percentage=$current_health/$max_health*100;

that works but returns the percentage with lots of decimals. is there a way to round the result up?


sorry,

in real terms i think it would be something like this.

'current health' divided by 'max health' times 100 = percentage

im just not sure how i would do that in php
 
Last edited:
Back
Top Bottom