如何将数字分解为百分比 (0 - 100%)?里面有详细信息
我正在使用一个 JS 进度条,该进度条使用百分比设置:0 到 100(百分比)。当 160,000 人签署了某个表格时,我需要进度条达到 100%。我在 PHP 变量中设置了签名者总数,但不知道如何进行数学计算,将其转换为 1 - 100 范围内的百分比(这样进度条实际上反映了 160,000 的目标)。
我可能在这里遗漏了一些明显的东西(我对任何与数字相关的东西都很感兴趣)所以这里有人知道如何做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只是
Just
如果 N 是目标,X 是迄今为止已签名的人数,则百分比为 (X/N)*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:
160,000/160,000
=1
=100%
160,000/2
=0.5
= < code>50%鉴于此,计算应该很容易。分子是已完成的数字,分母是“目标”——要完成的总数。
所以一旦你达到 80,000,你就完成了 50%,你的进度条也会这样呈现。
如果您需要使用较小的数字,请将所有内容除以 100 或 1000,这样您就可以减少相关庄园中数字的大小。
160,000 / 1000 = 160
160 将是您的分母。因此,50%
将为80/160
。这有道理吗?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 than50%
would be80/160
. Does this make sense?...你不能将数字转换为百分比吗?
如果您不想要浮动答案,您可以根据需要进行转换或舍入。
...you can't convert a number to a percentage?
If you don't want a float answer you can just cast or round it however you'd like.
百分比计算是基础数学:
Percentage calculation is basic mathematics: