# C# Math equation help

I have a c# program with the following variables:

MinVoltage
MaxVoltage
MinValue
MaxValue

The VALUE could be %, Celcius, Fahrenheit Litres etc
The VOLTAGE could or could not be inverted (i.e. minVolt = 4.5 and maxVolt=0.5)

The user would set the min/max VALUE i.e.
MinValue:  0     (%)
MaxValue: 100 (%)

in the equation, i know the CURRENT VOLTAGE reading, and I need to translate that to the VALUE (in this example %)
The curve ball is that sometimes the min and max voltage is inverted, and other times its not

I need a C# (or basic maths equation to help output the VALUE)

In this example:
minVolt =  4.5
maxVolt = 0.5
minVal =    0      (%)
maxVal =   100  (%)

currentVoltage = 2.25

i.e. pseduo code is:
``````Function GetValueFromVoltage(decimal currentVoltage, decimal minVolt, decimal maxVolt, decimal minVal, decimal maxVal)
{
boolean IsInverted = false;

if (minVolt > maxVolt) {
IsInverted =true;
}

//Here I need to figure out how to work out percentage/litres/temperature C/F etc
currentValue = ?

Return currentValue
}
``````

Can anyone help me with the calculation please, my brain isn't working today after a long weekend :)

although the example above is using %, I will never know if it is %, C, F, Litres etc
I will only know min and max VALUES, min and Max VOLTAGE and CURRENT VOLTAGE
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
//assuming a linear relationship
currentVal = minVal+(maxVal-minVal)*(currentVolt-minVolt)/(maxVolt-minVolt)

Experts Exchange Solution brought to you by