I have a value that i need to translate to a percentage given a certain set of rules.
VALUE=X
Where X can be anything starting from 0
If:
X > 200
the result of my function should be 100 (percent).
If:
X < 200 AND >= 100
the result should be between 100 and 50 (percent).
Example: X=150 would be 75%
If:
X < 100 AND >= 80
the result should be between 50 and 25 (percent).
Example: X=90 would be 37.5%
And if:
X < 80
the result should be between 25 and 0 (percent).
My approach in PHP would be something like
if($value > 200) return 100;
if($value > 100 && $value < 200) return ???;
… and so on.
Wherein ??? obviously stands for the formula i don’t know how to set up.
Is there a way to do this in one single formula?
And even if not – what is the mathematical approach to this problem?
I know it is very basic but it seems i skipped too many maths lessons in primary school.
The function can be defined piecewisely:
Graphically, this is represented with four straight lines of different gradients. While there isn’t a way to represent this function without separate pieces, implementing the above is quite easy.
In PHP, this function can be written as: