Map numbers from one range to another


February 23rd, 2021



mapValue.ts

For example, mapValue(1, 0, 2, 0, 10) will result in 5.

function mapValue(
  value: number,
  valueRangeStart: number,
  valueRangeEnd: number,
  newRangeStart: number,
  newRangeEnd: number
) {
  return (
    newRangeStart +
    ((newRangeEnd - newRangeStart) /
      (valueRangeEnd - valueRangeStart)) *
      (value - valueRangeStart)
  );
}

This site is open source. Improve this page »