开发者

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

开发者 https://www.devze.com 2022-12-28 22:44 出处:网络
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 n

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?


Percentage calculation is basic mathematics:

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


Just

percentage = number/160000 * 100


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


Do it Microsoft style:

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


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?


...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.

0

精彩评论

暂无评论...
验证码 换一张
取 消