AJ.
AJ.

Reputation: 643

How do i break a number down into a percentage (0 - 100%)? Details inside

I am using a JS progress bar that is set using a percentage: 0 to 100 (percent). I need the progress bar to reach 100% when 160,000 people have signed a certain form. I have the total number of signers set in a PHP variable but am lost on how to do the math to convert that into a percentage that fits within 1 - 100 (so that the progress bar actually reflects the goal of 160,000).

I may be missing something obvious here (i suck at anything number-related) so does anyone here have a clue as to how to do this?

Upvotes: 0

Views: 8761

Answers (6)

Paul
Paul

Reputation: 2759

...you can't convert a number to a percentage?

$percent = ($currentNumber / 160000) * 100; 

If you don't want a float answer you can just cast or round it however you'd like.

Upvotes: 0

Frank V
Frank V

Reputation: 25419

160,000/160,000 = 1 = 100%

160,000/2 = 0.5 = 50%

Given this, the calculation should be easy. The numerator is the number completed and the denominator is the "goal" -- the total to complete.

So once you are 80,000 you'd be 50% complete and your progress bar would render as such.

If you need to work with smaller numbers, divide everything by 100 or 1000 and you can reduce the size of the numbers in a relevant manor.

160,000 / 1000 = 160 160 would be your denominator. So than 50% would be 80/160. Does this make sense?

Upvotes: 0

Dan Piponi
Dan Piponi

Reputation: 8116

Do it Microsoft style:

current = 12345
percentage = 100*(1-exp(-current/100000))

Upvotes: 0

Gumbo
Gumbo

Reputation: 655189

Percentage calculation is basic mathematics:

$total = 160000;
$current = 12345;
$percentage = $current/$total * 100;

Upvotes: 5

David Z
David Z

Reputation: 131550

Just

percentage = number/160000 * 100

Upvotes: 1

duffymo
duffymo

Reputation: 308743

If N is the goal, and X is the number of people that have signed so far, then the percentage is (X/N)*100.

Upvotes: 0

Related Questions