SOLVED: Show score as a percentage

Started by Slasher, Sun 31/03/2013 14:06:23

Previous topic - Next topic

Slasher

Hi

l have a label that shows a score. It works fine. What I am trying to do is show the Percentage of a score on the label.

Let's say that the total maximum score is 10 and you have 6: the percentage would show 60% etc etc

How can I adopt this into a Label?

Code: AGS

 Ltotal.Text = String.Format("You have rescued %d of the missing children.",Children); // show percent not int


Cheers and thanks if you can help

PS I'm going to try Children/100. I know it won't decimalise but that's ok/

Not sure if that would work?


selmiak

if you really ever have 10 children to save as a max just add a 0 and the % sign to the variable in the string. otherwise you calculate this by numberofsavedchildren / maxnumberofchildren * 100. rounding and floattoint might come in useful here too.

Slasher

There may be more than 10????

I will give a whirl.

cheers



Gilbert

Quote from: selmiak on Sun 31/03/2013 14:28:56
numberofsavedchildren / maxnumberofchildren * 100
Actually if you just want to do it with integer maths, the correct way is:
(numberofsavedchildren * 100) / maxnumberofchildren

As as long as numberofsavedchildren < maxnumberofchildren, numberofsavedchildren / maxnumberofchildren yields zero.
(I think by default the operator precedence is left-to-right now, but for safety I'll just add the parentheses to ensure the multiplication comes first.)

Slasher

This seems to be working but have yet checked its accuracy (int not float).

Code: AGS
   
Ltotal.Text = String.Format("You have saved %d percent of the missing children.",(Children*100)/maxchildren);


Cheers Iceboty V7000a

While I'm here, is their a way to show the % sign?


Crimson Wizard

Quote from: slasher on Sun 31/03/2013 16:57:37
While I'm here, is their a way to show the % sign?

All is in the manual (see "String formatting").

"%%".

selmiak

%% should show only one % on screen.

Slasher

Such a ninny, yeh, just seen that %%

Hit me over the head with a slice of lemon  8-)

Wrapped up and solved.

Ghost

Quote from: slasher on Sun 31/03/2013 17:23:55
Hit me over the head with a slice of lemon  8-)

You DID wrap it around a gold bar first, right?  :-D

SMF spam blocked by CleanTalk