How to calculate percentage between the range of two values a third value is

后端 未结 4 661
忘掉有多难
忘掉有多难 2021-01-30 00:40

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 va

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 01:11

    If you want to calculate the percentages of a list of values and truncate the values between a max and min you can do something like this:

    private getPercentages(arr:number[], min:number=0, max:number=100): number[] {
      let maxValue = Math.max( ...arr );
    
      return arr.map((el)=>{
        let percent = el * 100 / maxValue;
        return percent * ((max - min) / 100) + min;
      });
    };
    

    Here the function call:

    this.getPercentages([20,30,80,200],20,50);

    would return

    [23, 24.5, 32, 50]
    

    where the percentages are relative and placed between the min and max value.

提交回复
热议问题