简体   繁体   English

如何计算三个值在两个值的范围之间的百分比

[英]How to calculate percentage between the range of two values a third value is in

Example: 例:

I'm trying to figure out the calculation for finding the percentage between two values that a third value is. 我试图找出用于计算第三个值是两个值之间的百分比的计算。

Example: The range is -46 to 195. The value -46 would 0%, and the value 195 would be 100% of the range. 示例:范围是-46到195。值-46将为0%,值195将为范围的100%。 What percentage of this range is the value 65? 65是这个范围的百分之几?

valueMin=-46 valueMin = -46

valueMax=195 valueMax = 195

valueNow=65 valueNow = 65

valuePercentage = ? valuePercentage =?

I have tried the following algorithm but it doesn't work : 我已经尝试了以下算法,但是不起作用:

const log = (txt) => console.log(txt, 'instead of 100%');

function getValueNowInPercent(valueMin, valueMax, valueNow){
  const diff = valueMax - valueMin;
  const percent = valueNow / diff * 100;  
  return percent + '%';
}

let valueMin, valueMax, valueNow;

valueMin = 0; valueNow = 50; valueMax = 100;
log(getValueNowInPercent(valueMin,valueNow,valueMax));
==> 200% instead of 100%

valueMin = -100; valueNow = 0; valueMax = 100;
log(getValueNowInPercent(valueMin,valueNow,valueMax));
==> 100% instead of 100%

valueMin = 0; valueNow = 100; valueMax = 200;
log(getValueNowInPercent(valueMin,valueNow,valueMax));
==> 200% instead of 100%

valueMin = -200; valueNow = 0; valueMax = 200;
log(getValueNowInPercent(valueMin,valueNow,valueMax));
==> 100% instead of 100%

How about mapping this into domain range problem where domain would be your min and max values and range will be from 0 to 100 如何将其映射到域范围问题,其中domain是您的最小和最大值,范围从0到100

 function mapBetween(currentNum, minAllowed, maxAllowed, min, max) { return (maxAllowed - minAllowed) * (currentNum- min) / (max - min) + minAllowed; } console.log (mapBetween(-46,0,100,-46,195)) console.log (mapBetween(195,0,100,-46,195)) console.log (mapBetween(65,0,100,-46,195)) console.log (mapBetween(100,0,100,0,200)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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