admin管理员组文章数量:1328034
I have this progress bar in JS and I'm filling up progress by increasing its width using this code:
function move(ValueSC) {
var elem = document.getElementById("scaleOrig");
var width = 0;
var internalVlue = ValueSC;
var id = setInterval(frame(internalVlue), 50);
function frame(internalVlue) {
if (width >= internalVlue) {
clearInterval(id);
} else {
width += internalVlue;
elem.style.width = width + '%';
}
}
}
This works perfectly but the problem is that I will get a value (result) from my calculator which is something between numbers 15 to 40, so with this stated 15 will be 0% and 40 will 100%. I want the width of progress bar to go up to 100 percent if the result (from my calculator) is 40 and if it is 15 the value of progress bar should be 0%. Basically I need the logic to convert all the 25 numbers from 15 to 40 to have a percentage value between 0 to 100%.
I have this progress bar in JS and I'm filling up progress by increasing its width using this code:
function move(ValueSC) {
var elem = document.getElementById("scaleOrig");
var width = 0;
var internalVlue = ValueSC;
var id = setInterval(frame(internalVlue), 50);
function frame(internalVlue) {
if (width >= internalVlue) {
clearInterval(id);
} else {
width += internalVlue;
elem.style.width = width + '%';
}
}
}
This works perfectly but the problem is that I will get a value (result) from my calculator which is something between numbers 15 to 40, so with this stated 15 will be 0% and 40 will 100%. I want the width of progress bar to go up to 100 percent if the result (from my calculator) is 40 and if it is 15 the value of progress bar should be 0%. Basically I need the logic to convert all the 25 numbers from 15 to 40 to have a percentage value between 0 to 100%.
Share Improve this question edited Jul 24, 2016 at 6:37 user663031 asked Jul 24, 2016 at 6:21 Zakriya BilalZakriya Bilal 1352 silver badges8 bronze badges 5- On one number increase, percentage should go up by 4% – Rahul Arora Commented Jul 24, 2016 at 6:24
- @RahulArora Yes, That is correct – Zakriya Bilal Commented Jul 24, 2016 at 6:25
- 1 So whatever value you get, you just have to apply this logic, (value-15)*4 and if I am not wrong this should be the width – Rahul Arora Commented Jul 24, 2016 at 6:27
- @RahulArora I think this works! – Zakriya Bilal Commented Jul 24, 2016 at 6:29
- This is just basic arithmetic, which is definitely possible. – user663031 Commented Jul 24, 2016 at 6:33
4 Answers
Reset to default 8As a broader answer, the formula you're looking for is ((value - min) / (max - min)) * 100
. max
and min
are the bounds of your value and value
is the current progress. This will return the percentage of the progress so far for any given value between two other values.
var percent = ((value - 15) / (40 - 15)) * 100
40
will give you 100
and 15
will give you 0
Before you call your method do this:
ValueSC= ((Max-Min)/100)*(Current-Min))
Where: Current: Your value Min = Minimum Value Max = Max Value
First you have to divide up 100 to smaller steps. There can be 25 (40-15) different values for your number, so the length of a step should be 100 / 25, and your starting number is 15. Something like this should work:
function getPercentage(number) {
return (number - 15) * (100 / 25);
}
本文标签: mathJavascript logic get percentage of each number between two numbersStack Overflow
版权声明:本文标题:math - Javascript logic get percentage of each number between two numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742242825a2438939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论