哈士奇WWW
这里有一些用于复制粘贴的JavaScript(这是烦人的答案):function scaleBetween(unscaledNum, minAllowed, maxAllowed, min, max) { return (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed;}像这样应用,将范围10-50缩放到0-100之间的范围。var unscaledNums = [10, 13, 25, 28, 43, 50];var maxRange = Math.max.apply(Math, unscaledNums);var minRange = Math.min.apply(Math, unscaledNums);for (var i = 0; i < unscaledNums.length; i++) { var unscaled = unscaledNums[i]; var scaled = scaleBetween(unscaled, 0, 100, minRange, maxRange); console.log(scaled.toFixed(2));}0.00,18.37,48.98,55.10,85.71,100.00编辑:我知道我很久以前就回答了这个问题,但是我现在使用的是一个更清洁的功能:Array.prototype.scaleBetween = function(scaledMin, scaledMax) { var max = Math.max.apply(Math, this); var min = Math.min.apply(Math, this); return this.map(num => (scaledMax-scaledMin)*(num-min)/(max-min)+scaledMin);}应用如下:[-4, 0, 5, 6, 9].scaleBetween(0, 100);[0,30.76923076923077,69.23076923076923,76.92307692307692,100]
慕后森
为方便起见,这里是Java形式的Irritate算法。根据需要添加错误检查,异常处理和调整。public class Algorithms { public static double scale(final double valueIn, final double baseMin, final double baseMax, final double limitMin, final double limitMax) { return ((limitMax - limitMin) * (valueIn - baseMin) / (baseMax - baseMin)) + limitMin; }}测试:final double baseMin = 0.0;final double baseMax = 360.0;final double limitMin = 90.0;final double limitMax = 270.0;double valueIn = 0;System.out.println(Algorithms.scale(valueIn, baseMin, baseMax, limitMin, limitMax));valueIn = 360;System.out.println(Algorithms.scale(valueIn, baseMin, baseMax, limitMin, limitMax));valueIn = 180;System.out.println(Algorithms.scale(valueIn, baseMin, baseMax, limitMin, limitMax));90.0270.0180.0