简体   繁体   中英

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?

Percentage calculation is basic mathematics:

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

只是

percentage = number/160000 * 100

如果目标是N,而X是到目前为止已签署的人数,则百分比是(X / N)* 100。

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

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?

Do it Microsoft style:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM