Trasnlate int value 100 to 0 and 0 to 100

Advertisements

Hey i would like to find a good way how i can best translate an intenger value. i have a method TranslateValue in which comes a value 100. now the weird part of the number hundred is to increment the who from 0 to 100 and if the value that comes in the method is for example 0 then the value is 100 which the method returns. so in short i want to have a counter method that calculates the other way around i have failed many times but i can’t find a good way that works

       int currentValue = 0;
    private int TranslateValue(int valueToTranslate)
    {
        if (valueToTranslate > 0)
        {
            currentValue++;
        }
        else
        {
            currentValue--;
        }
    }

>Solution :

To map a value from 0 to 100 to the inverse range 100..0, simply

return 100 - value;

You may wish to add suitable checks around that, or just clamp the output to 0..100 with

return Math.Min(100, Math.Max(0, 100 - value));

Leave a ReplyCancel reply