Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trasnlate int value 100 to 0 and 0 to 100

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading