Page 1 of 1

percentage code

Posted: Mon May 29, 2006 2:51 am
by dspain
looking for the best way to return percentages in bc 5.01 for wg3 modules.

i know for a fact from school the formula is:

(currentvalue) / (maxvalue) * 100

but im having a hell of a time, i think cause when ya do the first division statement it breaks it down into decimal

ie: 25/100 comes out tio 0.25

basically i got a mud i been coding on for over a year and i want to return statements where if i look at a monster as someone holding the sysop key after the monster description i want to add a line for vitality info..

ie:
blah blah
Vitality: 25/100 [25%]

i made several functions all return 0's cause of the floating point i do believe but who knows thats why im asking.

sean ferrell had a percnt function in ta1 but it was embedded in the core a snippet of that code would be awesome.

Posted: Tue May 30, 2006 1:17 am
by Questman
Here you go:

int pct(int current, int maxval) {
int ret;

ret = ((float)current / (float)maxval)*100.0;
return ret;
}

as such, calling it like this

int curhealth=25;
int maxhealth=100;

prf("Your health is %d/%d [%f%%]\n",curhealth,maxhealth,pct(curhealth,maxhealth));

returns:

Your health is 25/100 [25%]