Possible Duplicate:
Convert a number range to another range, maintaining ratio
So I have a function that returns values within 0 and 255 and I need to convert these values to something between -255 and 255 So 200 would be roughly 145, 150 would be roughly 45 and so on.. I have looked at Convert a number range to another range, maintaining ratio but the formulas there won't work. Any other formula I could use?
To convert a number range to another range, maintaining the ratio the simple way would be use linear conversion.
Click anywhere in the table and then go to Table Tools > Design on the Ribbon. In the Tools group, click Convert to Range. Right-click the table, then in the shortcut menu, click Table > Convert to Range. Note: Table features are no longer available after you convert the table back to a range.
Use Paste Special and Multiply Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.
If you need to map or translate inputs to arbitrary values, you can use the VLOOKUP function. Since there is no way to derive the output (i.e. it's arbitrary), we need to do some kind of lookup. The VLOOKUP function provides an easy way to do this.
public static int ConvertRange(
int originalStart, int originalEnd, // original range
int newStart, int newEnd, // desired range
int value) // value to convert
{
double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
return (int)(newStart + ((value - originalStart) * scale));
}
Try this:
int Adjust( int num )
{
return num * 2 - 255;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With