How to match numbers in C #, for example, with a map in Arduino?

Is there a feature like Arduino map in C #?

+10
c # arduino
source share
2 answers

You can do this with an extension method (e.g. for decimal ):

 public static class ExtensionMethods { public static decimal Map (this decimal value, decimal fromSource, decimal toSource, decimal fromTarget, decimal toTarget) { return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget; } } 

Then you can use it like:

 decimal res = 2.Map(1, 3, 0, 10); // res will be 5 
+17
source share
 private static int map(int value, int fromLow, int fromHigh, int toLow, int toHigh) { return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; } 
0
source share

All Articles